signifix

Number Formatter of Fixed Significance with Metric or Binary Prefix

Build Status Downloads Rust Version Documentation License

Formats a given number in one of the three Signifix notations as defined below by determining

  1. the appropriate metric or binary prefix and
  2. the decimal mark position in such a way as to sustain a fixed number of four significant figures.

Contents

Signifix Notations

Three notations are defined,

With Metric Prefix

The two Signifix notations with metric prefix comprise

In default notation the placeholder is another whitespace as in ±1.234␣␣ to align consistently, while in alternate notation it is a number sign as in ±1#234 to conspicuously separate the integer from the fractional part of the significand. The locale-sensitive decimal mark defaults to a decimal point. The plus sign of positive numbers is optional.

With Binary Prefix

The one Signifix notation with binary prefix comprises

To align consistently, the placeholder is another two whitespaces as in ±1.234␣␣␣. The locale-sensitive decimal mark defaults to a decimal point while the locale-sensitive thousands separator defaults to a whitespace as in ±1␣023␣Ki. The plus sign of positive numbers is optional.

Usage

This crate works since Rust 1.34 on stable channel. It is on crates.io and can be used by adding signifix to the dependencies in your project's Cargo.toml:

toml [dependencies] signifix = "0.10"

Examples

The Notations

The Signifix notations result in a fixed number of characters preventing jumps to the left or right while making maximum use of their occupied space:

```rust use std::convert::TryFrom;

use signifix::{metric, binary, Result};

let metric = |number| -> Result<(String, String)> { let number = metric::Signifix::tryfrom(number)?; Ok((format!("{}", number), format!("{:#}", number))) }; let binary = |number| -> Result { let number = binary::Signifix::tryfrom(number)?; Ok(format!("{}", number)) };

// Three different decimal mark positions covering the three powers of ten // of a particular metric prefix. asserteq!(metric(1E-04), Ok(("100.0 µ".into(), "100µ0".into()))); // 3rd asserteq!(metric(1E-03), Ok(("1.000 m".into(), "1m000".into()))); // 1st asserteq!(metric(1E-02), Ok(("10.00 m".into(), "10m00".into()))); // 2nd asserteq!(metric(1E-01), Ok(("100.0 m".into(), "100m0".into()))); // 3rd asserteq!(metric(1E+00), Ok(("1.000 ".into(), "1#000".into()))); // 1st asserteq!(metric(1E+01), Ok(("10.00 ".into(), "10#00".into()))); // 2nd asserteq!(metric(1E+02), Ok(("100.0 ".into(), "100#0".into()))); // 3rd asserteq!(metric(1E+03), Ok(("1.000 k".into(), "1k000".into()))); // 1st asserteq!(metric(1E+04), Ok(("10.00 k".into(), "10k00".into()))); // 2nd asserteq!(metric(1E+05), Ok(("100.0 k".into(), "100k0".into()))); // 3rd assert_eq!(metric(1E+06), Ok(("1.000 M".into(), "1M000".into()))); // 1st

// Three different decimal mark positions and a thousands separator covering // the four powers of ten of a particular binary prefix. asserteq!(binary(1024f64.powi(0) * 1E+00), Ok("1.000 ".into())); // 1st asserteq!(binary(1024f64.powi(0) * 1E+01), Ok("10.00 ".into())); // 2nd asserteq!(binary(1024f64.powi(0) * 1E+02), Ok("100.0 ".into())); // 3rd asserteq!(binary(1024f64.powi(0) * 1E+03), Ok("1 000 ".into())); // 4th asserteq!(binary(1024f64.powi(1) * 1E+00), Ok("1.000 Ki".into())); // 1st asserteq!(binary(1024f64.powi(1) * 1E+01), Ok("10.00 Ki".into())); // 2nd asserteq!(binary(1024f64.powi(1) * 1E+02), Ok("100.0 Ki".into())); // 3rd asserteq!(binary(1024f64.powi(1) * 1E+03), Ok("1 000 Ki".into())); // 4th asserteq!(binary(1024f64.powi(2) * 1E+00), Ok("1.000 Mi".into())); // 1st

// Rounding over prefixes is safe against floating-point inaccuracies. asserteq!(metric(999.9499999999998), Ok(("999.9 ".into(), "999#9".into()))); asserteq!(metric(999.9499999999999), Ok(("1.000 k".into(), "1k000".into()))); asserteq!(binary(1023.49999999999994), Ok("1 023 ".into())); asserteq!(binary(1023.49999999999995), Ok("1.000 Ki".into())); ```

Transfer Rate

This is useful to smoothly refresh a transfer rate within a terminal:

```rust use std::convert::TryFrom;

use std::f64; use std::time::Duration; use signifix::metric::{Signifix, Error, DEFMINLEN};

let transferrate = |bytes: u64, duration: Duration| -> String { let seconds = duration.assecs() as f64 + duration.subsecnanos() as f64 * 1E-09; let bytespersecond = bytes as f64 / seconds; let unit = "B/s"; let rate = match Signifix::tryfrom(bytespersecond) { Ok(rate) => if rate.factor() < 1E+00 { " - slow - ".into() // instead of mB/s, µB/s, ... } else { format!("{}{}", rate, unit) // normal rate }, Err(case) => match case { Error::OutOfLowerBound(rate) => if rate == 0f64 { " - idle - " // no progress at all } else { " - slow - " // almost no progress }, Error::OutOfUpperBound(rate) => if rate == f64::INFINITY { " - ---- - " // zero nanoseconds } else { " - fast - " // awkwardly fast }, Error::Nan => " - ---- - ", // zero bytes in zero nanoseconds }.into(), }; debugasserteq!(rate.chars().count(), DEFMINLEN + unit.chars().count()); rate };

asserteq!(transferrate(42667, Duration::fromsecs(300)), "142.2 B/s"); asserteq!(transferrate(42667, Duration::fromsecs(030)), "1.422 kB/s"); asserteq!(transferrate(42667, Duration::fromsecs(003)), "14.22 kB/s"); asserteq!(transferrate(00001, Duration::fromsecs(003)), " - slow - "); asserteq!(transferrate(00000, Duration::fromsecs(003)), " - idle - "); asserteq!(transferrate(42667, Duration::fromsecs(000)), " - ---- - "); ```

Measured Amps

Or to monitor a measured quantity like an electrical current including its direction with positive numbers being padded to align with negative ones:

```rust use std::convert::TryFrom;

use signifix::metric::{Signifix, Result, DEFMAXLEN};

let measuredamps = |amps| -> Result { if let Some(amps) = amps { Signifix::tryfrom(amps) .map(|amps| format!("{:>1$}A", amps, DEFMAXLEN)) } else { Ok(" 0 A".into()) } };

asserteq!(measuredamps(Some( 1.476E-06)), Ok(" 1.476 µA".into())); asserteq!(measuredamps(None), Ok(" 0 A".into())); asserteq!(measuredamps(Some(-2.927E-06)), Ok("-2.927 µA".into())); ```

Filesize Diff

While to visualize a change in file size, a plus sign might be preferred for positive numbers:

```rust use std::convert::TryFrom;

use signifix::metric::{Signifix, Error, Result};

let filesizediff = |curr, prev| -> Result { Signifix::tryfrom(curr - prev).map(|diff| format!("{:+#}", diff)) .or_else(|case| if case == Error::OutOfLowerBound(0f64) { Ok("=const".into()) } else { Err(case) }) };

asserteq!(filesizediff(78346, 57393), Ok("+20k95".into())); asserteq!(filesizediff(93837, 93837), Ok("=const".into())); asserteq!(filesizediff(27473, 36839), Ok("-9k366".into())); ```

Boundary Stat

The binary prefix instead suits well to visualize quantities being multiples of powers of two, such as memory boundaries due to binary addressing:

```rust use std::convert::TryFrom;

use signifix::binary::{Signifix, Error, Result};

let boundarystat = |used: u64, size: u64| -> Result { if used == 0 { let size = Signifix::tryfrom(size)?; return Ok(format!(" 0 B ( 0 %) of {}B", size)); } let p100 = Signifix::tryfrom(used as f64 / size as f64 * 100.0) .map(|p100| format!("{:.*} %", p100.exponent(), p100.significand())) .orelse(|error| if let Error::OutOfLowerBound() = error { Ok(" < 1 %".into()) } else { Err(error) })?; let used = Signifix::tryfrom(used)?; let size = Signifix::try_from(size)?; Ok(format!("{}B ({}) of {}B", used, p100, size)) };

asserteq!(boundarystat(0000u64.pow(1), 1024u64.pow(3)), Ok(" 0 B ( 0 %) of 1.000 GiB".into())); asserteq!(boundarystat(1024u64.pow(2), 1024u64.pow(3)), Ok("1.000 MiB ( < 1 %) of 1.000 GiB".into())); asserteq!(boundarystat(3292u64.pow(2), 1024u64.pow(3)), Ok("10.34 MiB (1.009 %) of 1.000 GiB".into())); asserteq!(boundarystat(8192u64.pow(2), 1024u64.pow(3)), Ok("64.00 MiB (6.250 %) of 1.000 GiB".into())); asserteq!(boundarystat(1000u64.pow(3), 1024u64.pow(3)), Ok("953.7 MiB (93.13 %) of 1.000 GiB".into())); asserteq!(boundarystat(1024u64.pow(3), 1024u64.pow(3)), Ok("1.000 GiB (100.0 %) of 1.000 GiB".into())); ```

Localizations

Until there is a recommended and possibly implicit localization system for Rust, explicit localization can be achieved by wrapping the Signifix type into a locale-sensitive newtype which implements the Display trait via the Signifix::fmt() method:

```rust use std::convert::TryFrom;

use signifix::binary::{Signifix, Result};

struct SignifixSi(Signifix); // English SI style (default) struct SignifixEn(Signifix); // English locale (whitespace -> comma) struct SignifixDe(Signifix); // German locale (comma <-> point)

impl std::fmt::Display for SignifixSi { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { std::fmt::Display::fmt(&self.0, f) } } impl std::fmt::Display for SignifixEn { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { self.0.fmt(f, ".", ",") } } impl std::fmt::Display for SignifixDe { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { self.0.fmt(f, ",", ".") } }

let localizations = |number| -> Result<(String, String, String)> { Signifix::try_from(number).map(|number| ( format!("{}", SignifixSi(number)), format!("{}", SignifixEn(number)), format!("{}", SignifixDe(number)), )) };

asserteq!(localizations(999.9f64 * 1024f64), Ok(("999.9 Ki".into(), "999.9 Ki".into(), "999,9 Ki".into()))); asserteq!(localizations(1000f64 * 1_024f64), Ok(("1 000 Ki".into(), "1,000 Ki".into(), "1.000 Ki".into()))); ```

Customization

Customization can be achieved by extracting information from the Signifix type via its methods:

```rust use std::convert::TryFrom;

use signifix::metric::{Signifix, Result};

struct SignifixTable<'a>(&'a[Signifix]);

impl<'a> std::fmt::Display for SignifixTable<'a> { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { f.pad(" Int Fra 10³\n")?; f.pad("---- ---- ----\n")?; for entry in self.0 { let (integer, fractional) = entry.parts(); f.pad(&format!("{:4} {:<3} {:2}\n", integer, fractional, entry.prefix() as i32 - 8))?; } Ok(()) } }

let customization = |entries: &[]| -> Result { let mut table = Vec::withcapacity(entries.len()); for entry in entries { table.push(Signifix::tryfrom(*entry)?); } Ok(SignifixTable(&table).tostring()) };

assert_eq!(customization(&[ 1.234E-06, 12.34E+00, -123.4E+24, ]), Ok(concat!( " Int Fra 10³\n", "---- ---- ----\n", " 1 234 -2\n", " 12 34 0\n", "-123 4 8\n", ).into())); ```

License

Copyright (c) 2016-2019 Rouven Spreckels n3vu0r@qu1x.org

Usage of the works is permitted provided that this instrument is retained with the works, so that any entity that uses the works is notified of this instrument.

DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the works by you shall be licensed as above, without any additional terms or conditions.