#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

#1505 in Rust patterns

Download history 2273/week @ 2023-12-04 2702/week @ 2023-12-11 1330/week @ 2023-12-18 525/week @ 2023-12-25 2162/week @ 2024-01-01 2666/week @ 2024-01-08 4051/week @ 2024-01-15 2881/week @ 2024-01-22 2411/week @ 2024-01-29 3748/week @ 2024-02-05 2853/week @ 2024-02-12 1931/week @ 2024-02-19 2574/week @ 2024-02-26 2598/week @ 2024-03-04 2464/week @ 2024-03-11 2441/week @ 2024-03-18

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