Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zignis/abbrev-num
Abbreviate numbers into a human-friendly format
https://github.com/zignis/abbrev-num
Last synced: 27 days ago
JSON representation
Abbreviate numbers into a human-friendly format
- Host: GitHub
- URL: https://github.com/zignis/abbrev-num
- Owner: zignis
- License: mit
- Created: 2024-04-07T12:52:27.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-04-07T15:14:23.000Z (7 months ago)
- Last Synced: 2024-09-12T22:36:21.614Z (about 2 months ago)
- Language: Rust
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# abbrev-num
Abbreviate numbers into a human-friendly format
# Examples
## Basic
```rust
use abbrev_num::abbrev_num;assert_eq!(abbrev_num(1_400, None), Some("1.4k".to_string()));
```## Precision
```rust
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()));
```## Custom units
```rust
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()));
```## Custom rounding strategy
```rust
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()));
```