#numbers #formatting #separators #commas

thousands

Adds digit separators to numbers, configurably

6 releases

Uses old Rust 2015

0.2.0 Oct 20, 2019
0.1.4 Oct 20, 2019
0.1.2 Sep 18, 2018

#47 in Value formatting

Download history 74954/week @ 2023-10-31 60952/week @ 2023-11-07 65720/week @ 2023-11-14 61272/week @ 2023-11-21 71409/week @ 2023-11-28 71808/week @ 2023-12-05 53881/week @ 2023-12-12 37831/week @ 2023-12-19 21310/week @ 2023-12-26 40323/week @ 2024-01-02 48574/week @ 2024-01-09 56788/week @ 2024-01-16 57457/week @ 2024-01-23 53715/week @ 2024-01-30 59839/week @ 2024-02-06 47755/week @ 2024-02-13

230,718 downloads per month
Used in 118 crates (47 directly)

MIT/Apache

22KB
376 lines

thousands

Build Status Crates.io License: MIT License: Apache 2.0

Provides a trait, Separable, for formatting numbers with separators between the digits. Typically this will be used to add commas or spaces every three digits from the right, but can be configured via a SeparatorPolicy.

Examples

The simplest way to use the library is with trait Separable’s method separate_with_commas method, which does what it sounds like:

use thousands::Separable;

 assert_eq!(   12345  .separate_with_commas(),  "12,345" );
 assert_eq!( (-12345) .separate_with_commas(), "-12,345" );
 assert_eq!(    9876.5.separate_with_commas(),   "9,876.5" );

There are also methods separate_with_spaces, separate_with_dots, and separate_with_underscores, in case you, your culture, or your file format prefer those separators.

However, it's also possible to pass a policy for different behavior:

use thousands::{Separable, SeparatorPolicy, digits};

let policy = SeparatorPolicy {
    separator: ',',
    groups:    &[3, 2],
    digits:    digits::ASCII_DECIMAL,
};

assert_eq!( 1234567890.separate_by_policy(policy), "1,23,45,67,890" );

Usage

It’s on crates.io, so you can add

[dependencies]
thousands = "0.2.0"

to your Cargo.toml.

This crate supports Rust version 1.22 and newer.

No runtime deps