Skip to main content

Crate numeral_format

Crate numeral_format 

Source
Expand description

§numeral-format — format numbers with the numeral.js format-string DSL

Format numbers using the familiar numeral.js mini-language — thousands separators, fixed/optional decimals, abbreviations, currency, percentages, bytes, ordinals, forced signs, and parenthesized negatives:

use numeral_format::format;

assert_eq!(format(1000.0, "0,0"), "1,000");
assert_eq!(format(1000.234, "0,0.00"), "1,000.23");
assert_eq!(format(1_234_567.0, "0.0a"), "1.2m");
assert_eq!(format(0.974_878, "0.000%"), "97.488%");
assert_eq!(format(-1000.0, "($0,0)"), "($1,000)");
assert_eq!(format(1000.0, "0b"), "1KB");
assert_eq!(format(2.0, "0o"), "2nd");

A faithful Rust port of the formatting half of the widely-used numeral npm package (the default en locale), which has no Rust equivalent. Zero dependencies.

§Scope

This crate ports number → string formatting with the default en locale. It does not implement parsing formatted strings back to numbers (unformat), arithmetic helpers, or alternate locales. NaN is treated as numeral’s null (formatted as zero).

Functions§

format
Format value using a numeral.js format string (default "0,0" when empty).