scaler
Introduction
This crate provides a convenient Formatter
to scale, round, and display numbers.
Scaling describes the usage of decimal / metric / SI unit prefixes or binary / IEC unit prefixes to increase readability; though no scaling and scientific notation are also supported.
Rounding can be done either to a specified magnitude or to a number of significant digits.
Separators can be freely adjusted. The group separator separates groups of digits every 3 digits before the decimal separator, while the decimal separator separates the integer and fractional parts of a number.
The sign behaviour can be set to only show the sign when the number is negative ("-"), which is the default, or always show the sign ("+" and "-"). The latter can be useful for highlighting differences.
By default rounding can create trailing zeros. They can optionally be removed.
Installation
The feature warn_about_problematic_separators
warns using log::warn!
if separators are being set with Formatter::set_separators
that could lead to ambiguous formatting. It depends on the log
crate and is the only dependency. If a dependencyless build should be desired, it can be disabled by specifying default-features = false
in your Cargo.toml entry.
Usage
- Execute
Formatter::new
to create a newFormatter
with default settings. - Adjust separators, rounding, scaling, and sign behaviour as necessary using the setters.
- Format numbers with
Formatter::format
.
Rounding
Examples have scaling disabled for easier understanding.
-
Magnitude
:- Round to digit at magnitude $10^m$.
- Contains $m$.
let f: Formatter = new .set_scaling .set_rounding; assert_eq!; assert_eq!; assert_eq!;
let f: Formatter = new .set_scaling .set_rounding; assert_eq!; assert_eq!; assert_eq!;
let f: Formatter = new .set_scaling .set_rounding; assert_eq!; assert_eq!; assert_eq!;
let f: Formatter = new .set_scaling .set_rounding; assert_eq!; assert_eq!; assert_eq!;
let f: Formatter = new .set_scaling .set_rounding; assert_eq!; assert_eq!; assert_eq!;
-
SignificantDigits
:- Round to $n$ significant numbers.
- Contains $n$.
let f: Formatter = new .set_scaling .set_rounding; assert_eq!; assert_eq!; assert_eq!;
let f: Formatter = new .set_scaling .set_rounding; assert_eq!; assert_eq!; assert_eq!;
let f: Formatter = new .set_scaling .set_rounding; assert_eq!; assert_eq!; assert_eq!;
let f: Formatter = new .set_scaling .set_rounding; assert_eq!; assert_eq!; assert_eq!;
let f: Formatter = new .set_scaling .set_rounding; assert_eq!; assert_eq!; assert_eq!;
let f: Formatter = new .set_scaling .set_rounding; assert_eq!; assert_eq!; assert_eq!;
Scaling
-
Binary
:- Scales by factor $2^(10) = 1024$.
- If no prefix for that magnitude defined: Fallback to scientific notation.
- Contains whether or not to put space between number and unit prefix.
let f: Formatter = new .set_scaling; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;
let f: Formatter = new .set_scaling; assert_eq!;
-
Decimal
:- Scales by factor $10^(3) = 1000$.
- If no prefix for that magnitude defined: Fallback to scientific notation.
- Contains whether or not to put space between number and unit prefix.
let f: Formatter = new .set_scaling; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;
let f: Formatter = new .set_scaling; assert_eq!;
-
None
:- no scaling
- no fallback to scientific notation
let f: Formatter = new .set_scaling; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;
-
Scientific
:- always scientific notation
let f: Formatter = new .set_scaling; assert_eq!; assert_eq!; assert_eq!;
Separators
group_separator
- Separates groups every 3 digits before the decimal separator.
decimal_separator
- Separates the integer and fractional parts of a number.
Examples have scaling disabled for easier understanding.
let f: Formatter = new
.set_scaling
.set_separators;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
let f: Formatter = new
.set_scaling
.set_separators;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
let f: Formatter = new
.set_scaling
.set_separators;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Sign
-
Always
- Always show sign, even when number is positive.
let f: Formatter = new .set_sign; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;
-
OnlyMinus
- Only show sign when number is negative.
let f: Formatter = new .set_sign; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;
Trailing Zeros
-
true
let f: Formatter = new .set_trailing_zeros; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;
-
false
let f: Formatter = new .set_trailing_zeros; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;