6 releases

Uses old Rust 2015

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

#42 in Value formatting

Download history 66296/week @ 2024-07-28 71302/week @ 2024-08-04 80242/week @ 2024-08-11 71678/week @ 2024-08-18 81414/week @ 2024-08-25 84788/week @ 2024-09-01 76957/week @ 2024-09-08 69553/week @ 2024-09-15 79597/week @ 2024-09-22 80592/week @ 2024-09-29 87416/week @ 2024-10-06 84582/week @ 2024-10-13 92792/week @ 2024-10-20 79726/week @ 2024-10-27 96645/week @ 2024-11-03 80519/week @ 2024-11-10

353,475 downloads per month
Used in 155 crates (58 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