#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

#6 in #thousands

Download history 2133/week @ 2024-03-14 3417/week @ 2024-03-21 2639/week @ 2024-03-28 2062/week @ 2024-04-04 2169/week @ 2024-04-11 2872/week @ 2024-04-18 2558/week @ 2024-04-25 2596/week @ 2024-05-02 2409/week @ 2024-05-09 2299/week @ 2024-05-16 1416/week @ 2024-05-23 2487/week @ 2024-05-30 3365/week @ 2024-06-06 2172/week @ 2024-06-13 2311/week @ 2024-06-20 1885/week @ 2024-06-27

10,179 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