1 个不稳定版本
0.1.0 | 2024年4月7日 |
---|
#11 in #num
8KB
109 代码行
abbrev-num
将数字缩写为易于阅读的格式
示例
基本
use abbrev_num::abbrev_num;
assert_eq!(abbrev_num(1_400, None), Some("1.4k".to_string()));
精度
use abbrev_num::{abbrev_num, Options};
let options = Options {
precision: Some(2),
..Default::default()
};
assert_eq!(abbrev_num(1_420, Some(options)), Some("1.42k".to_string()));
自定义单位
use abbrev_num::{abbrev_num, Options};
let units: [&str; 7] = ["mm", "cm", "m", "km", "", "", ""];
let options = Options {
abbreviations: Some(units),
..Default::default()
};
assert_eq!(abbrev_num(1_400, Some(options)), Some("1.4cm".to_string()));
自定义舍入策略
use abbrev_num::{abbrev_num, Options, RoundingStrategy};
let options = Options {
rounding_strategy: Some(RoundingStrategy::ToZero),
precision: Some(0),
..Default::default()
};
assert_eq!(abbrev_num(1_566_450, Some(options)), Some("1M".to_string()));
依赖
~610KB
~11K SLoC