#comma #formatting #separator

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

#83 in Value formatting

Download history 144842/week @ 2025-10-28 155706/week @ 2025-11-04 140432/week @ 2025-11-11 194832/week @ 2025-11-18 134557/week @ 2025-11-25 173469/week @ 2025-12-02 162512/week @ 2025-12-09 151658/week @ 2025-12-16 75917/week @ 2025-12-23 80471/week @ 2025-12-30 166656/week @ 2026-01-06 170266/week @ 2026-01-13 176351/week @ 2026-01-20 178851/week @ 2026-01-27 205060/week @ 2026-02-03 197426/week @ 2026-02-10

784,801 downloads per month
Used in 346 crates (81 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