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
f64::NANoutputsNANf64::INFINITYoutputsINFINITY
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
impl Percent
sourcepub fn inf() -> Self
pub fn inf() -> Self
Returns a Self with the f64 value of f64::INFINITY.
The String is set to ∞.
sourcepub fn new_0_point(f: f64) -> Self
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
| Input | String Output |
|---|---|
| 0.0 | 0% |
| 50.123 | 50% |
| 100.1 | 100% |
sourcepub fn new_1_point(f: f64) -> Self
pub fn new_1_point(f: f64) -> Self
Same as Self::from but with 1 floating point.
sourcepub fn new_3_point(f: f64) -> Self
pub fn new_3_point(f: f64) -> Self
Same as Self::from but with 3 floating point.
sourcepub fn new_4_point(f: f64) -> Self
pub fn new_4_point(f: f64) -> Self
Same as Self::from but with 4 floating point.
sourcepub fn new_5_point(f: f64) -> Self
pub fn new_5_point(f: f64) -> Self
Same as Self::from but with 5 floating point.
sourcepub fn new_6_point(f: f64) -> Self
pub fn new_6_point(f: f64) -> Self
Same as Self::from but with 6 floating point.
Trait Implementations§
source§impl<'de> Deserialize<'de> for Percent
impl<'de> Deserialize<'de> for Percent
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,
source§impl PartialEq<&Percent> for f64
impl PartialEq<&Percent> for f64
source§impl PartialEq<&Percent> for str
impl PartialEq<&Percent> for str
source§impl PartialEq<&str> for Percent
impl PartialEq<&str> for Percent
source§impl PartialEq<Percent> for Percent
impl PartialEq<Percent> for Percent
source§impl PartialEq<Percent> for f64
impl PartialEq<Percent> for f64
source§impl PartialEq<Percent> for str
impl PartialEq<Percent> for str
source§impl PartialOrd<Percent> for Percent
impl PartialOrd<Percent> for Percent
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moreimpl StructuralPartialEq for Percent
Auto Trait Implementations§
impl RefUnwindSafe for Percent
impl Send for Percent
impl Sync for Percent
impl Unpin for Percent
impl UnwindSafe for Percent
Blanket Implementations§
source§impl<S> ParseFormatted for Swhere
S: AsRef<str>,
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,
fn parse_formatted<F, N>(&self, format: &F) -> Result<N, Error>where F: Format, N: FromFormattedStr,
self (typically a formatted string) into a number (see Examples above).source§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere T: Display,
source§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString. Read more