pretty-num 0.1.2

A lightweight library for compactly formatting integers.
Documentation
  • Coverage
  • 100%
    3 out of 3 items documented2 out of 3 items with examples
  • Size
  • Source code size: 52.1 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 131.0 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 5s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • SleeplessOne1917/pretty-num
    6 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • SleeplessOne1917

prettty-num

Format integers into a compact social media style format, similar to using Intl.NumberFormat("en", { notation: "compact" }); as a number formatter in Javascript.

Usage

Simply import the PrettyNumber trait into your module to use the pretty_format method on any number that can convert into an i64:

use pretty_num::PrettyNumber;
// Integers with a magnitude less than 1,000 do not get compacted.
assert_eq!(534.pretty_format(), String::from("534"));

// Integers with a magnitude greater than or equal to 1,000 get compacted.
assert_eq!(15_000.pretty_format(), String::from("15k"));

// Integers will have a single decimal point when rounded.
assert_eq!(4_230_542.pretty_format(), String::from("4.2M"));

// Formatted numbers get rounded to a number without a decimal place when appropriate.
assert_eq!(5_031.pretty_format(), String::from("5k"));

// Also works with negative numbers.
assert_eq!((-25_621_783).pretty_format(), String::from("-25.6M"));

// Can go as high as trillions!
assert_eq!(36_777_121_590_100i64.pretty_format(), String::from("36.8T"));

Why use this instead of another number formatting crate?

There are several other number formatting libraries for Rust such as numfmt, human_format, si_format, and si-scale. All of these crates are more flexible than this one. However, all of them have a fixed number of decimals. If you want to, for example, have 12 formatted as "12" and 1500 formatted as "1.5k", you will not be able to do so: you can get "2.0" and "1.5k" or "2" and "2k", but they all use an exact number of significant digits/decimal points. If compact numbers that omit the decimal when appropriate is all you need, this is the crate for you. Otherwise, the crates mentioned above are likely more appropriate for your usecase.