Module si_scale::helpers

source ·
Expand description

The helpers functions provide number parsing and correct SI formatting for various units. They are probably the most used functions in this crate.

You can extend with your own units and formatting using the scale_fn!() macro.

The seconds() function parses a number into a Value and displays it using 3 decimals and the appropriate scale for seconds (UnitAndBelow), so that non-sensical scales such as kilo-seconds may not appear.

use si_scale::helpers::{seconds, seconds3};

let actual = format!("result is {}", seconds(1234.5678));
let expected = "result is 1234.5678 s";
assert_eq!(actual, expected);

let actual = format!("result is {:>10}", seconds3(12.3e-7));
let expected = "result is   1.230 µs";
assert_eq!(actual, expected);

The bytes1() function parses a number into a Value using base 1000 and displays it using 1 decimal and the appropriate scale for bytes (UnitAndAbove), so that non-sensical scales such as milli-bytes may not appear.

use si_scale::helpers::bytes1;

let actual = format!("result is {}", bytes1(12_345_678));
let expected = "result is 12.3 MB";
assert_eq!(actual, expected);

let actual = format!("result is {:>10}", bytes1(16));
let expected = "result is     16.0 B";
assert_eq!(actual, expected);

let actual = format!("result is {}", bytes1(0.12));
let expected = "result is 0.1 B";
assert_eq!(actual, expected);

The bibytes1() function parses a number into a Value using base 1024 and displays it using 1 decimal and the appropriate scale for bytes (UnitAndAbove), so that non-sensical scales such as milli-bytes may not appear.

use si_scale::helpers::bibytes1;

let actual = format!("result is {}", bibytes1(12_345_678));
let expected = "result is 11.8 MiB";
assert_eq!(actual, expected);
let actual = format!("result is {}", bibytes1(16 * 1024));
let expected = "result is 16.0 kiB";
assert_eq!(actual, expected);
let actual = format!("result is {:>10}", bibytes1(16));
let expected = "result is     16.0 B";
assert_eq!(actual, expected);
let actual = format!("result is {}", bibytes1(0.12));
let expected = "result is 0.1 B";
assert_eq!(actual, expected);

Functions

  • Print a value in bibytes.
  • Print a value in bibytes with 1 decimal.
  • Print a value in bibytes with 2 decimals.
  • Print a value in bytes.
  • Print a value in bytes with 1 decimal.
  • Print a value in bytes with 2 decimals.
  • Print a value in bytes with thousands separator.
  • Print a number without units.
  • Print a value in seconds.
  • Print a value in seconds with 3 decimals.