Type Alias NiceFloat

Source
pub type NiceFloat = NiceWrapper<SIZE>;
Expand description

NiceFloat provides a quick way to convert an f32 or f64 (up to the absolute equivalent of u64::MAX) into a formatted byte string for e.g. printing.

Commas are added for every (integer) thousand; decimals are rounded up to the nearest eight digits using a tie-to-even strategy.

Absolute values larger than u64::MAX will print as either > 18,446,744,073,709,551,615 or < -18,446,744,073,709,551,615.

Unlike the other Nice* helpers, this one supports negative values! It also contains special handling for NaN and infinity, and comes with float- specific formatting helpers like [NiceFloat::compact_str] and [NiceFloat::precise_str].

That’s it!

§Examples

use dactyl::NiceFloat;

let nice = NiceFloat::from(1234.5678_f64);
assert_eq!(nice.as_str(), "1,234.56780000");
assert_eq!(nice.compact_str(), "1,234.5678");
assert_eq!(nice.precise_str(2), "1,234.56");

Rust floats are imprecise, so you may see some fractional weirdness. Our previous example, lowered to 32 bits, illustrates the point:

use dactyl::NiceFloat;

let nice = NiceFloat::from(1234.5678_f32);
assert_eq!(nice.as_str(), "1,234.56774902");        // .xxx8 == .xxx74092?
assert_eq!(1234.5678_f32.to_string(), "1234.5677"); // std::fmt is wrong too.

§Traits

Rustdoc doesn’t do a good job at documenting type alias implementations, but NiceFloat has a bunch, including:

  • AsRef<[u8]>
  • AsRef<str>
  • Borrow<[u8]>
  • Borrow<str>
  • Clone
  • Copy
  • Default
  • Deref<Target=[u8]>
  • Display
  • Eq / PartialEq
  • Hash
  • Ord / PartialOrd

You can instantiate a NiceFloat with:

  • From<f64>
  • From<Option<f64>>
  • From<f32>
  • From<Option<f32>>
  • From<FloatKind>

When converting from a None, the result will be equivalent to zero.

Aliased Type§

struct NiceFloat { /* private fields */ }

Implementations§

Source§

impl NiceFloat

Source

pub const INFINITY: Self

§Infinity.

A value representing infinity. Note that no distinction is made between positive and negative varieties.

§Examples
use dactyl::NiceFloat;

assert_eq!(NiceFloat::INFINITY.as_str(), "∞");
assert_eq!(NiceFloat::from(f64::INFINITY).as_str(), "∞");
assert_eq!(NiceFloat::from(f64::NEG_INFINITY).as_str(), "∞");
Source

pub const NAN: Self

§NaN.

A value representing a Not-a-Number.

§Examples
use dactyl::NiceFloat;

assert_eq!(NiceFloat::NAN.as_str(), "NaN");
assert_eq!(NiceFloat::from(f64::NAN).as_str(), "NaN");
Source

pub const ZERO: Self

§Zero.

A value representing zero.

§Examples
use dactyl::NiceFloat;

assert_eq!(NiceFloat::ZERO.as_str(), "0.00000000");
assert_eq!(NiceFloat::from(0_f64).as_str(), "0.00000000");
Source

pub const fn overflow(neg: bool) -> Self

§Overflow.

This is used for values with integer components that do not fit within the u64 range.

§Examples
use dactyl::NiceFloat;

assert_eq!(NiceFloat::from(f64::MAX).as_str(), "> 18,446,744,073,709,551,615");
assert_eq!(NiceFloat::from(-f64::MAX).as_str(), "< -18,446,744,073,709,551,615");
Source

pub fn with_separator(num: f64, sep: u8, point: u8) -> Self

§New Instance w/ Custom Separator.

Create a new instance, defining any arbitrary ASCII byte as the thousands separator, and another for the decimal point.

If you’re good with American commas/periods, just use [NiceFloat::from] instead; it’s faster.

§Examples
use dactyl::NiceFloat;

assert_eq!(NiceFloat::from(1234.5678_f64).as_str(), "1,234.56780000");
assert_eq!(
    NiceFloat::with_separator(1234.5678_f64, b'.', b',').as_str(),
    "1.234,56780000",
);

// The punctuation is also honored for "special" values:
assert_eq!(
    NiceFloat::with_separator(0_f64, b'.', b',').as_str(),
    "0,00000000",
);
assert_eq!(
    NiceFloat::with_separator(f64::MAX, b'.', b',').as_str(),
    "> 18.446.744.073.709.551.615",
);
§Panics

This method will panic if the separator is invalid ASCII.

Source§

impl NiceFloat

Source

pub fn compact_bytes(&self) -> &[u8]

§Compact Bytes.

This returns a byte slice without trailing decimal zeroes. If the value has no fractional component at all, it will just return the integer portion.

§Examples
use dactyl::NiceFloat;

let nice = NiceFloat::from(12345.678_f64);
assert_eq!(nice.as_bytes(), b"12,345.67800000");
assert_eq!(nice.compact_bytes(), b"12,345.678"); // Some zeroes to trim.

let nice = NiceFloat::from(12340.0);
assert_eq!(nice.as_bytes(), b"12,340.00000000");
assert_eq!(nice.compact_bytes(), b"12,340"); // No fraction at all.

let nice = NiceFloat::from(12345.6783333333_f64);
assert_eq!(nice.as_bytes(), b"12,345.67833333");
assert_eq!(nice.compact_bytes(), b"12,345.67833333"); // Nothing to trim.
Source

pub fn compact_str(&self) -> &str

§Compact String.

This returns a string slice without trailing decimal zeroes. If the value has no fractional component at all, it will just return the integer portion.

§Examples
use dactyl::NiceFloat;

let nice = NiceFloat::from(12345.678_f64);
assert_eq!(nice.as_str(), "12,345.67800000");
assert_eq!(nice.compact_str(), "12,345.678"); // Some zeroes to trim.

let nice = NiceFloat::from(12345.0);
assert_eq!(nice.as_str(), "12,345.00000000");
assert_eq!(nice.compact_str(), "12,345"); // No fraction at all.

let nice = NiceFloat::from(12345.6783333333_f64);
assert_eq!(nice.as_str(), "12,345.67833333");
assert_eq!(nice.compact_str(), "12,345.67833333"); // Nothing to trim.
Source

pub fn precise_bytes(&self, precision: usize) -> &[u8]

§Precise Bytes.

This truncates the fractional part to the desired number of places, and returns the corresponding byte slice.

If the precision is zero, only the integer portion will be returned. Precisions >= 8 are meaningless, and return the equivalent of [NiceFloat::as_bytes].

§Examples
use dactyl::NiceFloat;

let nice = NiceFloat::from(12345.678_f64);
assert_eq!(nice.precise_bytes(0), b"12,345");
assert_eq!(nice.precise_bytes(1), b"12,345.6");
assert_eq!(nice.precise_bytes(2), b"12,345.67");
assert_eq!(nice.precise_bytes(3), b"12,345.678");
assert_eq!(nice.precise_bytes(4), b"12,345.6780");
assert_eq!(nice.precise_bytes(5), b"12,345.67800");
assert_eq!(nice.precise_bytes(6), b"12,345.678000");
assert_eq!(nice.precise_bytes(7), b"12,345.6780000");
assert_eq!(nice.precise_bytes(8), b"12,345.67800000");
Source

pub fn precise_str(&self, precision: usize) -> &str

§Precise String.

This truncates the fractional part to the desired number of places, and returns the corresponding string slice.

If the precision is zero, only the integer portion will be returned. Precisions >= 8 are meaningless, and return the equivalent of [NiceFloat::as_str].

§Examples
use dactyl::NiceFloat;

let nice = NiceFloat::from(12345.678_f64);
assert_eq!(nice.precise_str(0), "12,345");
assert_eq!(nice.precise_str(1), "12,345.6");
assert_eq!(nice.precise_str(2), "12,345.67");
assert_eq!(nice.precise_str(3), "12,345.678");
assert_eq!(nice.precise_str(4), "12,345.6780");
assert_eq!(nice.precise_str(5), "12,345.67800");
assert_eq!(nice.precise_str(6), "12,345.678000");
assert_eq!(nice.precise_str(7), "12,345.6780000");
assert_eq!(nice.precise_str(8), "12,345.67800000");

Trait Implementations§

Source§

impl Default for NiceFloat

Source§

fn default() -> Self

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

impl From<FloatKind> for NiceFloat

Source§

fn from(kind: FloatKind) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for NiceFloat

Source§

fn from(num: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for NiceFloat

Source§

fn from(num: f64) -> Self

Converts to this type from the input type.