pub struct Formatter<'a> { /* private fields */ }Expand description
Structure containing options and scales used to format numbers with the right scale preffix, separators and units.
Implementations§
Source§impl<'a> Formatter<'a>
impl<'a> Formatter<'a>
Sourcepub fn new(scales: Scales<'a>, options: Options<'a>) -> Self
pub fn new(scales: Scales<'a>, options: Options<'a>) -> Self
Create a formatter with a given scale set and some options
Sourcepub fn set_force_sign(&mut self, force_sign: bool)
pub fn set_force_sign(&mut self, force_sign: bool)
Forces the sign to be displayed.
Sourcepub fn with_force_sign(self, force_sign: bool) -> Self
pub fn with_force_sign(self, force_sign: bool) -> Self
Forces the sign to be displayed.
Sourcepub fn set_decimals(&mut self, decimals: usize)
pub fn set_decimals(&mut self, decimals: usize)
Sets the number of decimals to display.
Sourcepub fn with_decimals(self, decimals: usize) -> Self
pub fn with_decimals(self, decimals: usize) -> Self
Sets the number of decimals to display.
Sourcepub fn set_unit<U: Into<Cow<'a, str>>>(&mut self, unit: U)
pub fn set_unit<U: Into<Cow<'a, str>>>(&mut self, unit: U)
Sets the expected unit, like B for bytes or g for grams.
Sourcepub fn with_unit<U: Into<Cow<'a, str>>>(self, unit: U) -> Self
pub fn with_unit<U: Into<Cow<'a, str>>>(self, unit: U) -> Self
Sets the expected unit, like B for bytes or g for grams.
Sourcepub fn set_separator<U: Into<Cow<'a, str>>>(&mut self, separator: U)
pub fn set_separator<U: Into<Cow<'a, str>>>(&mut self, separator: U)
Sets the separator between the number and the preffix.
Sourcepub fn with_separator<U: Into<Cow<'a, str>>>(self, separator: U) -> Self
pub fn with_separator<U: Into<Cow<'a, str>>>(self, separator: U) -> Self
Sets the separator between the number and the preffix.
Sourcepub fn format(&'a self, value: f64) -> ScaledValue<'a>
pub fn format(&'a self, value: f64) -> ScaledValue<'a>
Formats a number and returns a scaled value that can be displayed.
Source§impl Formatter<'static>
impl Formatter<'static>
Sourcepub fn si() -> Self
pub fn si() -> Self
Formatter that uses the SI format style
use human_number::Formatter;
let formatter = Formatter::si();
let result = format!("{}", formatter.format(4_234.0));
assert_eq!(result, "4.23 k");
let result = format!("{}", formatter.format(0.012_34));
assert_eq!(result, "12.34 m");
let formatter = Formatter::si().with_unit("g").with_separator("").with_decimals(1);
let result = format!("{}", formatter.format(4_234.0));
assert_eq!(result, "4.2kg");
let result = format!("{}", formatter.format(0.012_34));
assert_eq!(result, "12.3mg");Sourcepub fn binary() -> Self
pub fn binary() -> Self
Formatter that uses the binary format style
use human_number::Formatter;
let formatter = Formatter::binary().with_unit("B");
let result = format!("{}", formatter.format(4_320_133.0));
assert_eq!(result, "4.12 MiB");Sourcepub fn empty() -> Self
pub fn empty() -> Self
Formatter that doesn’t use a scale
use human_number::Formatter;
let formatter = Formatter::empty();
let result = format!("{}", formatter.format(25_000.0));
assert_eq!(result, "25000.00");
let formatter = Formatter::empty().with_unit("%");
let result = format!("{}", formatter.format(25.0));
assert_eq!(result, "25.00 %");Trait Implementations§
impl<'a> StructuralPartialEq for Formatter<'a>
Auto Trait Implementations§
impl<'a> Freeze for Formatter<'a>
impl<'a> RefUnwindSafe for Formatter<'a>
impl<'a> Send for Formatter<'a>
impl<'a> Sync for Formatter<'a>
impl<'a> Unpin for Formatter<'a>
impl<'a> UnwindSafe for Formatter<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more