#group #separator #thousands #decimal #delimiter

digit_group

Provides grouping (thousands separators) for numeric types

1 unstable release

Uses old Rust 2015

0.1.0 Feb 6, 2017

#1591 in Rust patterns

Download history 3825/week @ 2024-07-21 2480/week @ 2024-07-28 2022/week @ 2024-08-04 3001/week @ 2024-08-11 1853/week @ 2024-08-18 2459/week @ 2024-08-25 2876/week @ 2024-09-01 2600/week @ 2024-09-08 2112/week @ 2024-09-15 2886/week @ 2024-09-22 2730/week @ 2024-09-29 2850/week @ 2024-10-06 3024/week @ 2024-10-13 1841/week @ 2024-10-20 2208/week @ 2024-10-27 1904/week @ 2024-11-03

9,041 downloads per month
Used in 2 crates

Apache-2.0

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");

No runtime deps