Number Formatter of Fixed Significance with Metric Prefix
Formats a given number in one of the two Signifix notations as defined below by
- selecting the appropriate metric prefix and
- moving the decimal point position in a way to sustain a fixed number of four significant figures.
Definition
The Signifix notation comprises
- a metrically normalized signed significand, that is a decimal of four
significant figures from
±1.000to±999.9to cover the three powers of ten of a particular metric prefix with the three different decimal point positions between these four figures, and - a metric prefix symbol or its placeholder in case of no prefix
- either being appended along with a whitespace as in
±1.234␣k, that is the default notation, - or replacing the decimal point of the significand as in
±1k234, that is the alternate notation.
- either being appended along with a whitespace as in
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 plus sign of positive numbers is optional.
Usage
This crate is on crates.io and can be
used by adding signifix to the dependencies in your project's
Cargo.toml:
[]
= "0.4.0"
and this to your crate root:
// Until stabilized. Requires nightly Rust.
extern crate signifix;
Examples
Both the default and alternate notation result in a fixed number of characters preventing jumps to the left or right:
#
use TryFrom; // Until stabilized.
use ;
let format = ;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
This is useful to smoothly refresh a transfer rate within a terminal:
// Until stabilized.
#
use TryFrom; // Until stabilized.
use f64;
use ;
let format_rate = ;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Or to monitor a measured quantity like an electrical current including its direction with positive numbers being padded to align with negative ones:
#
use TryFrom; // Until stabilized.
use ;
let format_load = ;
assert_eq!;
assert_eq!;
assert_eq!;
While to visualize a change in file size, a plus sign might be preferred for positive numbers:
#
use TryFrom; // Until stabilized.
use ;
let format_diff = ;
assert_eq!;
assert_eq!;
assert_eq!;