Crate thousands[][src]

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.

Usage

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

[dependencies]
thousands = "0.1.1"

to your Cargo.toml.

This crate supports Rust version 1.17 and newer.

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;

println!("x is {}", x.separate_with_commas());

There are also methods separate_with_spaces and separate_with_dots, in case your culture uses 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" );

Modules

digits

Collections of digits.

policies

Predefined policies.

Structs

SeparatorPolicy

A policy for inserting separators into numbers.

Traits

Separable

Provides methods for formatting numbers with separators between the digits.