use crate::RoundingMode;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub(crate) enum Precision {
Decimals(u8),
Significant(u8),
}
#[derive(Copy, Clone, Debug)]
pub struct NumberOptions {
pub(crate) precision: Precision,
pub(crate) compact: bool,
pub(crate) force_sign: bool,
pub(crate) rounding: RoundingMode,
pub(crate) long_units: bool,
pub(crate) separators: bool,
pub(crate) fixed_precision: bool,
pub(crate) decimal_separator: char,
pub(crate) group_separator: char,
}
impl NumberOptions {
#[inline]
pub const fn new() -> Self {
Self {
precision: Precision::Decimals(1),
compact: true,
force_sign: false,
rounding: RoundingMode::HalfUp,
long_units: false,
separators: false,
fixed_precision: false,
decimal_separator: '.',
group_separator: ',',
}
}
#[inline]
pub const fn precision(mut self, n: u8) -> Self {
let n = if n > 6 { 6 } else { n };
self.precision = Precision::Decimals(n);
self
}
#[inline]
pub const fn significant_digits(mut self, n: u8) -> Self {
let n = if n < 1 {
1
} else if n > 39 {
39
} else {
n
};
self.precision = Precision::Significant(n);
self
}
#[inline]
pub const fn compact(mut self, enabled: bool) -> Self {
self.compact = enabled;
self
}
#[inline]
pub const fn force_sign(mut self, yes: bool) -> Self {
self.force_sign = yes;
self
}
#[inline]
pub const fn rounding(mut self, mode: RoundingMode) -> Self {
self.rounding = mode;
self
}
#[inline]
pub const fn long_units(mut self) -> Self {
self.long_units = true;
self
}
#[inline]
pub const fn separators(mut self, yes: bool) -> Self {
self.separators = yes;
self
}
#[inline]
pub const fn fixed_precision(mut self, yes: bool) -> Self {
self.fixed_precision = yes;
self
}
#[inline]
pub const fn decimal_separator(mut self, sep: char) -> Self {
self.decimal_separator = sep;
self
}
#[inline]
pub const fn group_separator(mut self, sep: char) -> Self {
self.group_separator = sep;
self
}
}
impl Default for NumberOptions {
#[inline]
fn default() -> Self {
Self::new()
}
}