1 unstable release
Uses old Rust 2015
0.1.0 | Feb 6, 2017 |
---|
#6 in #thousands
10,179 downloads per month
Used in 2 crates
14KB
215 lines
digit_group
This is a small Rust crate that provides grouping (aka "thousands separators") for numeric types.
Usage
Cargo.toml
:
[dependencies]
digit_group = "0.1"
main.rs
:
extern crate digit_group;
use digit_group{FormatGroup,custom_group};
fn main() {
let x: f64 = 12345678.112233;
// Typical usage.
x.format_commas(); // 12,345,678.112233
x.format_si('.'); // 12 345 678.112 233
// Customizable groupings, decimal marks, and grouping delimiters.
x.format_custom('#',':',4,2, false); // 12:34:5678#112233
// Customizing precision prior to grouping.
let y = 5512.332;
let pre_formatted = format!("{:.4}", x);
custom_group(&pre_formatted, ',', ' ', 3, 3, false); // 5 512,3320
}
lib.rs
:
This crate provides grouping (aka "thousands separators") for numeric types.
Examples
Typical use-case to format a number with groups sized 3 (United States):
use digit_group::FormatGroup;
let x = 123456789;
assert_eq!(x.format_commas(), "123,456,789")
Formatting based on SI standards (with custom decimal mark):
use digit_group::FormatGroup;
let x: f64 = 123456789.01234;
assert_eq!(x.format_si('.'), "123 456 789.012 34")
Completely custom decimal mark, grouping delimiter, initial group size, and subsequent group size:
use digit_group::FormatGroup;
let x: f64 = 123456789.01;
assert_eq!(x.format_custom('#',':',4,2, false), "1:23:45:6789#01")
Using format!
to change the precision of a value prior to grouping:
use digit_group::custom_group;
let val: f64 = 111222.3;
let formatted = format!("{:.3}", val);
let grouped = custom_group(&formatted, ',', ' ', 3, 3, false);
assert_eq!(grouped, "111 222,300");