numan
Humanize numbers in Rust — turn raw numbers into the short, friendly strings you see in dashboards, CLIs and reports, and parse them back.
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Zero dependencies. #![no_std] (needs only alloc). One small, focused crate.
Why numan?
Rust's ecosystem already humanizes file sizes
(humansize) and relative time
(chrono-humanize). What was missing
is the part Python's humanize and Go's
go-humanize are famous for: shortening
the magnitude of plain numbers.
| Need | Reach for |
|---|---|
File sizes (1.2 MiB) |
humansize |
Relative time (3 hours ago) |
chrono-humanize |
Number → words (forty-two) |
num2words |
| Locale thousands grouping | num-format |
Compact / word / metric magnitude (1.2K, 1.2 million, 1.2 k) + parsing |
numan ✅ |
Install
[]
= "0.1"
Usage
Compact, word and metric notation
use ;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!; // SI: lowercase kilo
assert_eq!;
Ordinals
assert_eq!;
assert_eq!;
Parsing back
use parse;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!; // separators ignored
Great for CLI flags like --limit 1.5M.
Fine-grained control with Formatter
use Formatter;
let f = compact.precision.space;
assert_eq!;
// Keep trailing zeros, force an explicit sign:
let f = compact.trim_zeros.sign_plus;
assert_eq!;
| Builder method | Default | Effect |
|---|---|---|
precision(n) |
1 |
Max fractional digits |
trim_zeros(bool) |
true |
Drop trailing 0s and a dangling . |
space(bool) |
per-notation | Space between number and suffix |
sign_plus(bool) |
false |
Prefix non-negatives with + |
Behavior notes
- Rounding uses Rust's standard round-half-to-even, and correctly carries
into the next suffix (
compact(999_950)→"1M", never"1000K"). - Beyond the table,
compact(above a trillion) falls back to scientific notation (compact(1e36)→"1e36"), which still round-trips throughparse. - Non-finite floats render as
"NaN","inf","-inf". - Negative zero (and negatives that round to zero) render as
"0"— never"-0". - In
parse,m/Mmeans million (compact convention), never SI milli; SI exa (E) is not parsed, to avoid clashing with exponent notation (1e3), sometricoutput at exa returns an error rather than a wrong value.
no_std
numan is #![no_std] and only needs alloc (for String). It builds for
bare-metal targets such as thumbv7em-none-eabi.
Contributors ✨
This project follows the all-contributors specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.
Thanks goes to these wonderful people:
License
Licensed under either of Apache-2.0 or MIT at your option.