Struct Formatter

Source
pub struct Formatter<const BASE: usize = 0, const DECIMALS: usize = 2> { /* private fields */ }
Expand description

Entry point to the lib. Use this to handle your formatting needs.

  • BASE: the base
  • DECIMALS: target decimal places (if not keeping the original number)

Implementations§

Source§

impl Formatter

Source

pub const SI: Formatter<1000, 2>

SI units (western format).

Source

pub const BINARY: Formatter<1024, 2>

Binary units (western format).

Source

pub const CHINESE: Formatter<10000, 2>

Chinese units.

Source§

impl<const BASE: usize, const DECIMALS: usize> Formatter<BASE, DECIMALS>

Source

pub const fn new(units: &'static [&'static str]) -> Self

Create a new Formatter with given BASE, DECIMALS and units.

Source

pub const fn with_separator(self, separator: &'static str) -> Self

Set the separator between numbers and units.

Source

pub const fn with_scales<const N_BASE: usize>( self, units: &'static [&'static str], ) -> Formatter<N_BASE, DECIMALS>

Set scales, including base and the abbreviated number’s units.

Source

pub const fn with_custom_unit(self, custom_unit: &'static str) -> Self

Set custom unit attached after the abbreviated number’s unit.

Source

pub const fn with_decimals<const N_DECIMALS: usize>( self, ) -> Formatter<BASE, N_DECIMALS>

Set the decimal places to keep.

Source

pub fn format(&self, number: impl NumberT) -> FormatResult<DECIMALS>

Formats the given number into a human-readable string using the specified units and separator.

See NumberT for all types we accept as param.

§Notice

For better performance (may be so), you may need format_int or format_uint.

§Limitation

f64 can only handle 15 decimal places at most. We may introduce macro_toolset for large number formatting.

Source

pub fn format_int(&self, number: impl Into<i128>) -> FormatResult<DECIMALS>

Formats the given number into a human-readable string using the specified units and separator.

We accept any number that fits into isize. For i128, see format_large_int.

Source

pub fn format_uint(&self, number: impl Into<u128>) -> FormatResult<DECIMALS>

Formats the given number into a human-readable string using the specified units and separator.

Source

pub fn format_general( &self, integer: u128, fraction: Option<f64>, ) -> FormatResult<DECIMALS>

Formats the given number into a human-readable string using the specified units and separator.

§Params
  • integer: the integer part of the number.
  • fraction: the fractional part of the number.
§Notice

It’s NOT recommended that you use this directly, use format instead unless you know exactly what you do.

Source

pub fn format_float(&self, number: f64) -> FormatResult<DECIMALS>

Formats the given number into a human-readable string using the specified units and separator.

§Notice

It’s NOT recommended that you use this directly, floating point calculation often slower than integer arithmetic. Use format instead unless you know exactly what you do.

Trait Implementations§

Source§

impl<const BASE: usize, const DECIMALS: usize> Clone for Formatter<BASE, DECIMALS>

Source§

fn clone(&self) -> Formatter<BASE, DECIMALS>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const BASE: usize, const DECIMALS: usize> Debug for Formatter<BASE, DECIMALS>

Source§

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

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

impl<const BASE: usize, const DECIMALS: usize> Copy for Formatter<BASE, DECIMALS>

Auto Trait Implementations§

§

impl<const BASE: usize, const DECIMALS: usize> Freeze for Formatter<BASE, DECIMALS>

§

impl<const BASE: usize, const DECIMALS: usize> RefUnwindSafe for Formatter<BASE, DECIMALS>

§

impl<const BASE: usize, const DECIMALS: usize> Send for Formatter<BASE, DECIMALS>

§

impl<const BASE: usize, const DECIMALS: usize> Sync for Formatter<BASE, DECIMALS>

§

impl<const BASE: usize, const DECIMALS: usize> Unpin for Formatter<BASE, DECIMALS>

§

impl<const BASE: usize, const DECIMALS: usize> UnwindSafe for Formatter<BASE, DECIMALS>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.