Struct numfmt::Scales

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

Scale numbers.

Implementations§

source§

impl Scales

source

pub fn new(base: u16, units: Vec<&'static str>) -> Result<Self, Error>

Create a new scale.

If a unit is longer than the supported length, an error will be returned.

source

pub fn none() -> Self

Create a scale which is dummy and does not scale.

source

pub fn short() -> Self

The default scaling method.

Based on a short scale the scaling uses base 1000. The units are meant to be used to denote magnitude of the number, so the empty base is empty.

Example
let mut f = Formatter::default()
            .scales(Scales::short())
            .precision(Precision::Decimals(1));
assert_eq!(f.fmt(12.34e0), "12.3");
assert_eq!(f.fmt(12.34e3), "12.3 K");
assert_eq!(f.fmt(12.34e6), "12.3 M");
assert_eq!(f.fmt(12.34e9), "12.3 B");
assert_eq!(f.fmt(12.34e12), "12.3 T");
assert_eq!(f.fmt(12.34e15), "12.3 P");
assert_eq!(f.fmt(12.34e18), "12.3 E");
assert_eq!(f.fmt(12.34e21), "12.3 Z");
assert_eq!(f.fmt(12.34e24), "12.3 Y");
assert_eq!(f.fmt(12.34e27), "12,339.9 Y");
source

pub fn metric() -> Self

Create a metric SI scale.

The SI scale steps with base 1000. It is intended for use as a units prefix, so the empty base contains a space.

Example
let mut f = Formatter::new().scales(Scales::metric());
assert_eq!(f.fmt(123456.0), "123.456 k");
assert_eq!(f.fmt(123456789.0), "123.456789 M");
source

pub fn binary() -> Self

Create a binary scale.

The binary scale steps with base 1024. It is intended for use as a units prefix, so the empty base contains a space.

Example
let mut f = Formatter::new().scales(Scales::binary());
assert_eq!(f.fmt(1024.0 * 1024.0), "1.0 Mi");
assert_eq!(f.fmt(3.14 * 1024.0 * 1024.0), "3.14 Mi");
source

pub fn base(&self) -> u16

The set base.

source

pub fn units(&self) -> &[&'static str]

The set units.

source

pub fn into_inner(self) -> (u16, Vec<&'static str>)

Extract the (base, units).

source

pub fn scale(&self, num: f64) -> (f64, &'static str)

Scale a number and return the scaled number with the unit.

Trait Implementations§

source§

impl Clone for Scales

source§

fn clone(&self) -> Scales

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 Scales

source§

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

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

impl Hash for Scales

source§

fn hash<__H: Hasher>(&self, state: &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<Scales> for Scales

source§

fn eq(&self, other: &Scales) -> 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 Scales

source§

impl StructuralEq for Scales

source§

impl StructuralPartialEq for Scales

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.