Formatter

Struct Formatter 

Source
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

Source

pub fn new() -> Formatter

Initializes a new Formatter with default values.

Source

pub fn with_decimals(&mut self, decimals: usize) -> &mut Formatter

Sets the decimals value for formatting the string.

Source

pub fn with_separator(&mut self, separator: &str) -> &mut Formatter

Sets the separator value for formatting the string.

Source

pub fn with_scales(&mut self, scales: Scales) -> &mut Formatter

Sets the scales value.

Source

pub fn with_units(&mut self, units: &str) -> &mut Formatter

Sets the units value.

Source

pub fn with_suffix(&mut self, suffix: &str) -> &mut Formatter

Sets the expected suffix value.

Source

pub fn with_micro_sign(&mut self, enable: bool) -> &mut Formatter

Enable using the micro sign µ in formatted output when fractional suffix is u.

Source

pub fn format(&self, value: f64) -> String

Formats the number into a string

Source

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());
Source

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§

Source§

impl Debug for Formatter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Formatter

Source§

fn default() -> Formatter

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.