#pretty-print #float #format #print #human #pretty #string-formatting

float-pretty-print

Format f64 for showing to user, not for serialisation

2 releases

Uses old Rust 2015

0.1.1 Oct 2, 2021
0.1.0 May 7, 2019

#579 in Text processing

Download history 440/week @ 2023-12-06 215/week @ 2023-12-13 209/week @ 2023-12-20 121/week @ 2023-12-27 405/week @ 2024-01-03 267/week @ 2024-01-10 153/week @ 2024-01-17 319/week @ 2024-01-24 300/week @ 2024-01-31 65/week @ 2024-02-07 153/week @ 2024-02-14 182/week @ 2024-02-21 494/week @ 2024-02-28 668/week @ 2024-03-06 410/week @ 2024-03-13 210/week @ 2024-03-20

1,862 downloads per month
Used in 8 crates (5 directly)

MIT/Apache

315KB
5.5K SLoC

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:

  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

No runtime deps