#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

#5 in #thousands

Download history 2879/week @ 2024-09-21 2668/week @ 2024-09-28 2871/week @ 2024-10-05 3014/week @ 2024-10-12 1870/week @ 2024-10-19 2225/week @ 2024-10-26 1736/week @ 2024-11-02 4010/week @ 2024-11-09 3438/week @ 2024-11-16 2794/week @ 2024-11-23 2507/week @ 2024-11-30 1969/week @ 2024-12-07 3489/week @ 2024-12-14 1094/week @ 2024-12-21 3361/week @ 2024-12-28 4480/week @ 2025-01-04

12,722 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