#numbers #formatting #human #filesize #magnitude

no-std human_format

Rust Port of human-format from node, formatting numbers for us, while the machines are still at bay

5 stable releases

Uses old Rust 2015

1.1.0 Feb 17, 2024
1.0.3 Nov 24, 2019
1.0.2 Feb 2, 2018
1.0.1 Jan 29, 2018

#23 in Value formatting

Download history 11576/week @ 2024-03-14 10781/week @ 2024-03-21 11216/week @ 2024-03-28 10662/week @ 2024-04-04 11674/week @ 2024-04-11 10686/week @ 2024-04-18 9985/week @ 2024-04-25 10595/week @ 2024-05-02 12372/week @ 2024-05-09 13106/week @ 2024-05-16 13053/week @ 2024-05-23 13282/week @ 2024-05-30 13261/week @ 2024-06-06 14222/week @ 2024-06-13 14682/week @ 2024-06-20 10994/week @ 2024-06-27

55,714 downloads per month
Used in 45 crates (17 directly)

MIT license

14KB
197 lines

human-format-rs

Crates.io Documentation

Rust Port of human-format from node, formatting numbers for us, while the machines are still at bay.

Master Develop
Master Develop

What is human_format?

The primary purpose for this crate is to format numbers in a customizable fashion based around magnitudes. It is inspired by the human-format package and the hope is to ultimately provide an idiomatic rust port.

Usage

  1. Add this package as a dependency
[dependencies]
human_format = "1.0"
  1. Print some human readable strings

Examples

// 1.00 k
Formatter::new()
    .format(1000 as f64));

// 1.34 k
Formatter::new()
    .with_decimals(2)
    .format(1337 as f64);

// 1.3 k
Formatter::new()
    .with_decimals(1)
    .format(1337 as f64);

// 1.3B
Formatter::new()
    .with_decimals(1)
    .with_separator("")
    .format(1337000000 as f64);

// 1.00 - k
Formatter::new()
    .with_separator(" - ")
    .format(1000 as f64);


// Define your own scales as you see fit
let mut custom_binary_scales = Scales::new();

custom_binary_scales
    .with_base(1000)
    .with_suffixes(["".to_owned(),"k".to_owned(), "M".to_owned(), "G".to_owned(), "T".to_owned(), "P".to_owned(), "E".to_owned(), "Z".to_owned(), "Y".to_owned()].to_vec());

// 1.00 kB
Formatter::new()
    .with_scales(custom_binary_scales)
    .with_units("B")
    .format(1000 as f64);

// 1.00 kiB
Formatter::new()
    .with_scales(Scales::Binary())
    .with_units("B")
    .format(1000 as f64);

For more examples please consult tests/demo.rs

No runtime deps