Struct readable::Percent

source ·
pub struct Percent(_, _);
Expand description

Human readable percentage.

Takes a floating point number as input and returns a ready-to-print!() Percent.

The default From implementation will print 2 decimal numbers.

Anything lower than 0.01 is rounded down to 0.00.

This can be changed by using different functions when initially creating the Percent, or converting an existing Percent, for example:

let f0 = Percent::new_0_point(3.0);
let f2 = Percent::from(3.0);
let f3 = Percent::new_3_point(3.0);
let f4 = Percent::new_4_point(3.0);

assert!(f0 == "3%");
assert!(f2 == "3.00%");
assert!(f3 == "3.000%");
assert!(f4 == "3.0000%");

Performance

The actual string used internally is not a String, but a CompactString so that any string 24 bytes (12 bytes on 32-bit) or less are stack allocated instead of heap allocated.

The documentation will still refer to the inner string as a String. Anything returned will also be a String.

Exceptions

To disable checks for these, (you are sure you don’t have NaN’s), enable the ignore_nan_inf feature flag.

Examples

assert!(Percent::zero()    == "0.00%");
assert!(Percent::unknown() == "?.??%");

assert!(Percent::from(0.001)   == "0.00%");
assert!(Percent::from(0.1)     == "0.10%");
assert!(Percent::from(1.0)     == "1.00%");
assert!(Percent::from(100.0)   == "100.00%");
assert!(Percent::from(1_000.0) == "1,000.00%");

assert!(Percent::from(1_u64)      == "1.00%");
assert!(Percent::from(1_000_u64)  == "1,000.00%");
assert!(Percent::from(10_000_u64) == "10,000.00%");

assert!(Percent::from(-1_i64)      == "-1.00%");
assert!(Percent::from(-1_000_i64)  == "-1,000.00%");
assert!(Percent::from(-10_000_i64) == "-10,000.00%");

Implementations§

source§

impl Percent

source

pub fn zero() -> Self

Returns a Self with the f64 value of 0.0.

The String is set to 0.00%.

source

pub fn unknown() -> Self

Returns a Self with the f64 value of f64::NAN.

The String is set to ?.??%.

source

pub fn nan() -> Self

Returns a Self with the f64 value of f64::NAN.

The String is set to NaN.

source

pub fn inf() -> Self

Returns a Self with the f64 value of f64::INFINITY.

The String is set to .

source

pub fn new_0_point(f: f64) -> Self

Same as Self::from but with no floating point on the inner String.

The inner f64 stays the same as the input.

This does not round up or down, it completely ignores the floating point.

Examples
InputString Output
0.00%
50.12350%
100.1100%
source

pub fn new_1_point(f: f64) -> Self

Same as Self::from but with 1 floating point.

source

pub fn new_3_point(f: f64) -> Self

Same as Self::from but with 3 floating point.

source

pub fn new_4_point(f: f64) -> Self

Same as Self::from but with 4 floating point.

source

pub fn new_5_point(f: f64) -> Self

Same as Self::from but with 5 floating point.

source

pub fn new_6_point(f: f64) -> Self

Same as Self::from but with 6 floating point.

Trait Implementations§

source§

impl AsRef<str> for Percent

source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Borrow<str> for Percent

source§

fn borrow(&self) -> &str

Immutably borrows from an owned value. Read more
source§

impl Clone for Percent

source§

fn clone(&self) -> Percent

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 Percent

source§

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

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

impl<'de> Deserialize<'de> for Percent

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for Percent

source§

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

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

impl From<f32> for Percent

source§

fn from(number: f32) -> Self

Converts to this type from the input type.
source§

impl From<f64> for Percent

source§

fn from(number: f64) -> Self

Converts to this type from the input type.
source§

impl From<i16> for Percent

source§

fn from(number: i16) -> Self

Converts to this type from the input type.
source§

impl From<i32> for Percent

source§

fn from(number: i32) -> Self

Converts to this type from the input type.
source§

impl From<i64> for Percent

source§

fn from(number: i64) -> Self

Converts to this type from the input type.
source§

impl From<i8> for Percent

source§

fn from(number: i8) -> Self

Converts to this type from the input type.
source§

impl From<isize> for Percent

source§

fn from(number: isize) -> Self

Converts to this type from the input type.
source§

impl From<u16> for Percent

source§

fn from(number: u16) -> Self

Converts to this type from the input type.
source§

impl From<u32> for Percent

source§

fn from(number: u32) -> Self

Converts to this type from the input type.
source§

impl From<u64> for Percent

source§

fn from(number: u64) -> Self

Converts to this type from the input type.
source§

impl From<u8> for Percent

source§

fn from(number: u8) -> Self

Converts to this type from the input type.
source§

impl From<usize> for Percent

source§

fn from(number: usize) -> Self

Converts to this type from the input type.
source§

impl PartialEq<&Percent> for f64

source§

fn eq(&self, other: &&Percent) -> 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 PartialEq<&Percent> for str

source§

fn eq(&self, other: &&Percent) -> 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 PartialEq<&str> for Percent

source§

fn eq(&self, other: &&str) -> 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 PartialEq<Percent> for Percent

source§

fn eq(&self, other: &Percent) -> 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 PartialEq<Percent> for f64

source§

fn eq(&self, other: &Percent) -> 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 PartialEq<Percent> for str

source§

fn eq(&self, other: &Percent) -> 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 PartialEq<f64> for &Percent

source§

fn eq(&self, other: &f64) -> 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 PartialEq<f64> for Percent

source§

fn eq(&self, other: &f64) -> 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 PartialEq<str> for Percent

source§

fn eq(&self, other: &str) -> 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 PartialOrd<Percent> for Percent

source§

fn partial_cmp(&self, other: &Percent) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for Percent

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl StructuralPartialEq for Percent

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,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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<S> ParseFormatted for Swhere S: AsRef<str>,

source§

fn parse_formatted<F, N>(&self, format: &F) -> Result<N, Error>where F: Format, N: FromFormattedStr,

Converts self (typically a formatted string) into a number (see Examples above).
source§

impl<T> ToCompactString for Twhere T: Display,

source§

fn to_compact_string(&self) -> CompactString

Converts the given value to a CompactString. Read more
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> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. 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.
const: unstable · 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.
const: unstable · source§

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

Performs the conversion.
source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,