Struct numfmt::Formatter

source ·
pub struct Formatter { /* private fields */ }
Expand description

The number formatter configurations. See the module documentation for use.

Formatter has a FromStr implementation that can parse a string into a formatter using a specific grammar. Please consult the parsing section in the module documentation.

Implementations§

source§

impl Formatter

source

pub fn new() -> Self

Construct a new formatter.

No scaling is set, so this is only does a single allocation for the buffer.

Example
let mut f = Formatter::new();
assert_eq!(f.fmt(12345.6789), "12345.6789");
source

pub fn currency(prefix: &str) -> Result

Create a formatter that formats numbers as a currency.

Example
let mut f = Formatter::currency("$").unwrap();
assert_eq!(f.fmt(12345.6789), "$12,345.67");
assert_eq!(f.fmt(1234_f64), "$1,234.0");
source

pub fn percentage() -> Self

Create a formatter that formats numbers as a percentage.

Example
let mut f = Formatter::percentage();
assert_eq!(f.fmt(0.678912), "67.8912%");
assert_eq!(f.fmt(1.23), "123.0%");
assert_eq!(f.fmt(1.2), "120.0%");

f = f.precision(Precision::Decimals(2));
assert_eq!(f.fmt(0.01234), "1.23%");
source

pub fn convert(self, f: fn(_: f64) -> f64) -> Self

Set the value converter.

Use a converter to transform the input number into another number. This is done before all steps and the number follows the same procedure as normal. A good example of a use of a converter is to make a percentage number by always multiplying by 100.

source

pub fn precision(self, precision: Precision) -> Self

Set the precision.

source

pub fn scales(self, scales: Scales) -> Self

Set the scaling.

source

pub fn build_scales(self, base: u16, units: Vec<&'static str>) -> Result

Set the scaling via Scales::new.

source

pub fn separator<S: Into<Option<char>>>(self, sep: S) -> Result

Set the thousands separator.

If separator is not a single byte, an error is returned. If the separator is a period ., this signals to use a comma for the decimal marker.

Example
let mut f = Formatter::new().separator(',').unwrap(); // use a comma
assert_eq!(f.fmt(12345.67), "12,345.67");

f = f.separator(' ').unwrap(); // use a space
assert_eq!(f.fmt(12345.67), "12 345.67");

f = f.separator(None).unwrap(); // no separator
assert_eq!(f.fmt(12345.67), "12345.67");

f = f.separator('.').unwrap(); // use a period separator and comma for decimal
assert_eq!(f.fmt(12345.67), "12.345,67");
source

pub fn comma(self, comma: bool) -> Self

Set the comma option.

If set to true it will use a comma instead of a period. If a comma is the separator, a period will be used instead.

Example
let mut f = Formatter::new();
assert_eq!(f.fmt(12345.67), "12345.67");
f = f.comma(true);
assert_eq!(f.fmt(12345.67), "12345,67");

f = f.separator('.').unwrap();
assert_eq!(f.fmt(12345.67), "12.345,67");
source

pub fn prefix(self, prefix: &str) -> Result

Sets the prefix.

If the prefix is longer than the supported length, an error is returned.

source

pub fn suffix(self, suffix: &str) -> Result

Set the suffix.

If the suffix is longer than the supported length, an error is returned.

source

pub fn fmt(&mut self, num: f64) -> &str

👎Deprecated: consider using Formatter::fmt2 instead

Format the number!

source

pub fn fmt2<N: Numeric>(&mut self, num: N) -> &str

Format any number implementing Numeric.

Trait Implementations§

source§

impl Clone for Formatter

source§

fn clone(&self) -> Formatter

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 Debug for Formatter

source§

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

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

impl Default for Formatter

source§

fn default() -> Self

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

impl FromStr for Formatter

§

type Err = ParseError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, ParseError>

Parses a string s to return a value of this type. Read more
source§

impl Hash for Formatter

source§

fn hash<H: Hasher>(&self, hasher: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq<Formatter> for Formatter

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Formatter

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.