pub struct Formatter { /* private fields */ }Available on crate features
progress and unit-human only.Expand description
Entry point to the library. Use this to handle your formatting needs.
Implementations§
Source§impl Formatter
impl Formatter
Sourcepub fn with_decimals(&mut self, decimals: usize) -> &mut Formatter
pub fn with_decimals(&mut self, decimals: usize) -> &mut Formatter
Sets the decimals value for formatting the string.
Sourcepub fn with_separator(&mut self, separator: &str) -> &mut Formatter
pub fn with_separator(&mut self, separator: &str) -> &mut Formatter
Sets the separator value for formatting the string.
Sourcepub fn with_scales(&mut self, scales: Scales) -> &mut Formatter
pub fn with_scales(&mut self, scales: Scales) -> &mut Formatter
Sets the scales value.
Sourcepub fn with_units(&mut self, units: &str) -> &mut Formatter
pub fn with_units(&mut self, units: &str) -> &mut Formatter
Sets the units value.
Sourcepub fn with_suffix(&mut self, suffix: &str) -> &mut Formatter
pub fn with_suffix(&mut self, suffix: &str) -> &mut Formatter
Sets the expected suffix value.
Sourcepub fn with_micro_sign(&mut self, enable: bool) -> &mut Formatter
pub fn with_micro_sign(&mut self, enable: bool) -> &mut Formatter
Enable using the micro sign µ in formatted output when fractional suffix is u.
Sourcepub fn try_parse(&self, value: &str) -> Result<f64, ParseError>
pub fn try_parse(&self, value: &str) -> Result<f64, ParseError>
Attempt to parse a string back into a float value.
Examples:
use human_format::{Formatter, Scales};
// SI example
let f = Formatter::new();
assert_eq!(f.try_parse("1.00 k").unwrap(), 1000.0);
// Binary scales (ki = 1024)
let mut fbin = Formatter::new();
fbin.with_scales(Scales::Binary());
assert_eq!(fbin.try_parse("1.00 ki").unwrap(), 1024.0);
// Units specified via with_units() are automatically stripped from input
let mut funit = Formatter::new();
funit.with_units("B");
assert_eq!(funit.try_parse("1.00 kB").unwrap(), 1000.0);
// Negative numbers
assert_eq!(Formatter::new().try_parse("-1.0 k").unwrap(), -1000.0);
// Invalid input
assert!(Formatter::new().try_parse("bad input").is_err());Sourcepub fn parse_or_clamp(
&self,
value: &str,
clamp: bool,
) -> Result<f64, ParseError>
pub fn parse_or_clamp( &self, value: &str, clamp: bool, ) -> Result<f64, ParseError>
Parse a string and optionally clamp unknown suffixes to the largest suffix multiplier.
If clamp is false, this behaves like try_parse and returns an error on unknown suffixes.
If clamp is true, unknown suffixes will be interpreted as the largest available suffix.
Examples:
use human_format::{Formatter, Scales};
let f = Formatter::new();
// Unknown suffix errors when clamp == false
assert!(f.parse_or_clamp("1.0 DN", false).is_err());
// Unknown suffix clamps to largest suffix multiplier when clamp == true
assert!(f.parse_or_clamp("1.0 DN", true).is_ok());
// Binary example with units
let mut fb = Formatter::new();
fb.with_scales(Scales::Binary()).with_units("B");
assert_eq!(fb.parse_or_clamp("1.0 kiB", false).unwrap(), 1024.0);
// Negative number with clamp
assert_eq!(Formatter::new().parse_or_clamp("-1.0 k", true).unwrap(), -1000.0);Trait Implementations§
Auto Trait Implementations§
impl Freeze for Formatter
impl RefUnwindSafe for Formatter
impl Send for Formatter
impl Sync for Formatter
impl Unpin for Formatter
impl UnwindSafe for Formatter
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