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>
  • Clone
  • Copy
  • Default
  • Deref<Target=[u8]>
  • Display
  • Eq / PartialEq
  • Hash
  • Ord / PartialOrd

Implementations§

source§

impl NicePercent

source

pub const fn min() -> Self

Minimum value.

This reads: 0.00%.

source

pub const fn max() -> Self

Maximum value.

This reads: 100.00%.

Trait Implementations§

source§

impl Default for NicePercent

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl From<f32> for NicePercent

source§

fn from(num: f32) -> Self

Converts to this type from the input type.
source§

impl From<f64> for NicePercent

source§

fn from(num: f64) -> Self

Converts to this type from the input type.
source§

impl<T> TryFrom<(T, T)> for NicePercentwhere T: AsPrimitive<f64>,

source§

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.

§

type Error = ()

The type returned in the event of a conversion error.