Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vi/float-pretty-print
Format f64 in Rust with minimum and maximum width, auto switching between scientific and normal representation
https://github.com/vi/float-pretty-print
f64 float format human-readable pretty-print print rust string
Last synced: 2 days ago
JSON representation
Format f64 in Rust with minimum and maximum width, auto switching between scientific and normal representation
- Host: GitHub
- URL: https://github.com/vi/float-pretty-print
- Owner: vi
- Created: 2019-05-07T03:04:54.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-10-02T21:48:49.000Z (about 3 years ago)
- Last Synced: 2024-09-12T19:56:59.088Z (21 days ago)
- Topics: f64, float, format, human-readable, pretty-print, print, rust, string
- Language: Rust
- Size: 47.9 KB
- Stars: 10
- Watchers: 5
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# float-pretty-print
Round and format f64 number for showing it to humans, with configurable minimum and maximum width.
It automatically switches between exponential and usual forms as it sees fit.It works by trying usual `format!`, possibly multiple times and inspecting the resulting string.
Only two formatting parameters are supported:
* width is minimum width
* precision is maximum width`###` is printed if it can't output the number with given constraint.
Example:
```rust
use float_pretty_print::PrettyPrintFloat;
assert_eq!(format!("{}", PrettyPrintFloat(3.45)), "3.45");
assert_eq!(format!("{}", PrettyPrintFloat(12.0)), "12.0");
assert_eq!(format!("{}", PrettyPrintFloat(120000000.0)), "1.2e8");
assert_eq!(format!("{:5.5}", PrettyPrintFloat(12345.0)), "12345");
assert_eq!(format!("{:5.5}", PrettyPrintFloat(12.345)), "12.35");
assert_eq!(format!("{:5.5}", PrettyPrintFloat(0.12345)), "0.123");
assert_eq!(format!("{:5.5}", PrettyPrintFloat(1234500000.0)), "1.2e9");
assert_eq!(format!("{:5.5}", PrettyPrintFloat(12345.0e-19)), "1e-15");
assert_eq!(format!("{:5.5}", PrettyPrintFloat(12345.0e-100)), "1e-96");
assert_eq!(format!("{:5.5}", PrettyPrintFloat(12345.0e-130)), " 0");
assert_eq!(format!("{:5.5}", PrettyPrintFloat(12345.0e+130)), "1e134");
assert_eq!(format!("{:4.4}", PrettyPrintFloat(12345.0e+130)), "####");
assert_eq!(format!("{:6.6}", PrettyPrintFloat(12345.0e-130)), "1e-126");
```Supports even Rust 1.23