Type Definition dactyl::NicePercent
source · pub type NicePercent = NiceWrapper<SIZE>;Expand description
NicePercent provides a quick way to convert an f32 or f64 percent
— a value 0.0..=1.0 — into a formatted byte string for e.g. printing.
The precision is fixed at two decimal places (rounded at the thousandth),
with output ranging from 0.00% to 100.00%.
Inputs are expected to be in 0..=1. Values less than zero are clamped to
0.00%, while values greater than 1 are clamped to 100.00%.
For other types of floats, see NiceFloat instead.
That’s it!
Examples
use dactyl::NicePercent;
assert_eq!(NicePercent::from(0.321).as_str(), "32.10%");Traits
Rustdoc doesn’t do a good job at documenting type alias implementations, but
NicePercent has a bunch, including:
AsRef<[u8]>AsRef<str>Borrow<[u8]>Borrow<str>CloneCopyDefaultDeref<Target=[u8]>DisplayEq/PartialEqHashOrd/PartialOrd
Implementations§
source§impl NicePercent
impl NicePercent
sourcepub const fn min() -> Self
pub const fn min() -> Self
Minimum value.
This reads: 0.00%.
sourcepub const fn max() -> Self
pub const fn max() -> Self
Maximum value.
This reads: 100.00%.
Trait Implementations§
source§impl Default for NicePercent
impl Default for NicePercent
source§impl From<f32> for NicePercent
impl From<f32> for NicePercent
source§impl From<f64> for NicePercent
impl From<f64> for NicePercent
source§impl<T> TryFrom<(T, T)> for NicePercentwhere
T: AsPrimitive<f64>,
impl<T> TryFrom<(T, T)> for NicePercentwhere T: AsPrimitive<f64>,
source§fn try_from(src: (T, T)) -> Result<Self, Self::Error>
fn try_from(src: (T, T)) -> Result<Self, Self::Error>
Percent From T/T.
This method is a shorthand that performs the (decimal) division of
T1 / T2 for you, then converts the result into a NicePercent if
it falls between 0.0..=1.0.
use dactyl::NicePercent;
assert_eq!(
NicePercent::from(0.5_f64),
NicePercent::try_from((10_u8, 20_u8)).unwrap(),
);Errors
Conversion will fail if the enumerator is larger than the denominator, or if the denominator is zero.