pub struct NicePercent { /* private fields */ }Expand description
§Nice Percent.
This struct can be used to quickly and efficiently stringify a “percent” —
a float between 0.0..=1.0 — to a fixed precision of hundredths.
Values outside that range are “clamped” to make them make sense,
percentage-wise. (NaN and negative values are treated like zero; large
positives like one.)
§Examples
use dactyl::NicePercent;
// From a ready-made float:
assert_eq!(
NicePercent::from(0.55012345_f32).as_str(),
"55.01%",
);
// From separate "done" and "total" integers.
assert_eq!(
NicePercent::from((20_usize, 800_usize)).as_str(),
"2.50%",
);
// From weird shit.
assert_eq!(
NicePercent::from(-0.55012345_f32).as_str(), // Negative.
"0.00%",
);
assert_eq!(
NicePercent::from(f64::NAN).as_str(), // Not even a number!
"0.00%",
);
assert_eq!(
NicePercent::from(f64::NEG_INFINITY).as_str(), // Negative.
"0.00%",
);
assert_eq!(
NicePercent::from(55.012345_f32).as_str(), // Wrong scale.
"100.00%",
);
assert_eq!(
NicePercent::from(f64::INFINITY).as_str(), // So much more than one.
"100.00%",
);Implementations§
Source§impl NicePercent
impl NicePercent
Source§impl NicePercent
impl NicePercent
Source§impl NicePercent
impl NicePercent
Trait Implementations§
Source§impl AsRef<[u8]> for NicePercent
impl AsRef<[u8]> for NicePercent
Source§impl AsRef<str> for NicePercent
impl AsRef<str> for NicePercent
Source§impl Borrow<str> for NicePercent
impl Borrow<str> for NicePercent
Source§impl Clone for NicePercent
impl Clone for NicePercent
Source§fn clone(&self) -> NicePercent
fn clone(&self) -> NicePercent
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for NicePercent
Source§impl Debug for NicePercent
impl Debug for NicePercent
Source§impl Default for NicePercent
impl Default for NicePercent
Source§impl Display for NicePercent
impl Display for NicePercent
impl Eq for NicePercent
Source§impl From<(i8, i8)> for NicePercent
impl From<(i8, i8)> for NicePercent
Source§fn from((e, d): (i8, i8)) -> Self
fn from((e, d): (i8, i8)) -> Self
§Percent From i8/i8.
Create a NicePercent from the division of two integers
— e.g. a “done” and a “total” — leveraging
NiceFloat::div_i8
for the nitty-gritty.
Source§impl From<(i16, i16)> for NicePercent
impl From<(i16, i16)> for NicePercent
Source§fn from((e, d): (i16, i16)) -> Self
fn from((e, d): (i16, i16)) -> Self
§Percent From i16/i16.
Create a NicePercent from the division of two integers
— e.g. a “done” and a “total” — leveraging
NiceFloat::div_i16
for the nitty-gritty.
Source§impl From<(i32, i32)> for NicePercent
impl From<(i32, i32)> for NicePercent
Source§fn from((e, d): (i32, i32)) -> Self
fn from((e, d): (i32, i32)) -> Self
§Percent From i32/i32.
Create a NicePercent from the division of two integers
— e.g. a “done” and a “total” — leveraging
NiceFloat::div_i32
for the nitty-gritty.
Source§impl From<(i64, i64)> for NicePercent
impl From<(i64, i64)> for NicePercent
Source§fn from((e, d): (i64, i64)) -> Self
fn from((e, d): (i64, i64)) -> Self
§Percent From i64/i64.
Create a NicePercent from the division of two integers
— e.g. a “done” and a “total” — leveraging
NiceFloat::div_i64
for the nitty-gritty.
Source§impl From<(i128, i128)> for NicePercent
impl From<(i128, i128)> for NicePercent
Source§fn from((e, d): (i128, i128)) -> Self
fn from((e, d): (i128, i128)) -> Self
§Percent From i128/i128.
Create a NicePercent from the division of two integers
— e.g. a “done” and a “total” — leveraging
NiceFloat::div_i128
for the nitty-gritty.
Source§impl From<(isize, isize)> for NicePercent
impl From<(isize, isize)> for NicePercent
Source§fn from((e, d): (isize, isize)) -> Self
fn from((e, d): (isize, isize)) -> Self
§Percent From isize/isize.
Create a NicePercent from the division of two integers
— e.g. a “done” and a “total” — leveraging
NiceFloat::div_isize
for the nitty-gritty.
Source§impl From<(u8, u8)> for NicePercent
impl From<(u8, u8)> for NicePercent
Source§fn from((e, d): (u8, u8)) -> Self
fn from((e, d): (u8, u8)) -> Self
§Percent From u8/u8.
Create a NicePercent from the division of two integers
— e.g. a “done” and a “total” — leveraging
NiceFloat::div_u8
for the nitty-gritty.
Source§impl From<(u16, u16)> for NicePercent
impl From<(u16, u16)> for NicePercent
Source§fn from((e, d): (u16, u16)) -> Self
fn from((e, d): (u16, u16)) -> Self
§Percent From u16/u16.
Create a NicePercent from the division of two integers
— e.g. a “done” and a “total” — leveraging
NiceFloat::div_u16
for the nitty-gritty.
Source§impl From<(u32, u32)> for NicePercent
impl From<(u32, u32)> for NicePercent
Source§fn from((e, d): (u32, u32)) -> Self
fn from((e, d): (u32, u32)) -> Self
§Percent From u32/u32.
Create a NicePercent from the division of two integers
— e.g. a “done” and a “total” — leveraging
NiceFloat::div_u32
for the nitty-gritty.
Source§impl From<(u64, u64)> for NicePercent
impl From<(u64, u64)> for NicePercent
Source§fn from((e, d): (u64, u64)) -> Self
fn from((e, d): (u64, u64)) -> Self
§Percent From u64/u64.
Create a NicePercent from the division of two integers
— e.g. a “done” and a “total” — leveraging
NiceFloat::div_u64
for the nitty-gritty.
Source§impl From<(u128, u128)> for NicePercent
impl From<(u128, u128)> for NicePercent
Source§fn from((e, d): (u128, u128)) -> Self
fn from((e, d): (u128, u128)) -> Self
§Percent From u128/u128.
Create a NicePercent from the division of two integers
— e.g. a “done” and a “total” — leveraging
NiceFloat::div_u128
for the nitty-gritty.
Source§impl From<(usize, usize)> for NicePercent
impl From<(usize, usize)> for NicePercent
Source§fn from((e, d): (usize, usize)) -> Self
fn from((e, d): (usize, usize)) -> Self
§Percent From usize/usize.
Create a NicePercent from the division of two integers
— e.g. a “done” and a “total” — leveraging
NiceFloat::div_usize
for the nitty-gritty.