#string-conversion #float #display #double #floating-point #dtoa

pretty_dtoa

Configurable floating point number to string conversions, with many options for controlling various aspects of displaying floats

3 releases (breaking)

0.3.0 May 4, 2021
0.2.0 Mar 24, 2021
0.1.0 Jun 4, 2020

#167 in Value formatting

Download history 3115/week @ 2024-07-29 3598/week @ 2024-08-05 2763/week @ 2024-08-12 2984/week @ 2024-08-19 2731/week @ 2024-08-26 3119/week @ 2024-09-02 3473/week @ 2024-09-09 3139/week @ 2024-09-16 3987/week @ 2024-09-23 2680/week @ 2024-09-30 4314/week @ 2024-10-07 3375/week @ 2024-10-14 3087/week @ 2024-10-21 2788/week @ 2024-10-28 2515/week @ 2024-11-04 2051/week @ 2024-11-11

10,499 downloads per month
Used in 86 crates (8 directly)

MIT/Apache

36KB
694 lines

Pretty dtoa

Configurable float and double printing. pretty_dtoa Comes with lots of options for configuring different aspects of displaying floats, and has only 1 dependency total (including dependencies of dependencies), for very fast compile times.

This crate uses the ryu-floating-decimal crate (itself a fork of the ryu crate) to generate a "floating decimal", or a floating point with radix 10, and then it uses formatting rules particular to the configuration to create a formatted string.

This module is only slightly slow (usually between 1x and 2x slower than the default Display implementation for f64). Benchmarks can be run with cargo bench.

Consider using pretty_dtoa if the default behavior of Display and alternative float printing libraries like ryu is not ideal for one reason or another

Example

use pretty_dtoa::{dtoa, FmtFloatConfig};

let config = FmtFloatConfig::default()
     .force_no_e_notation()      // Don't use scientific notation
     .add_point_zero(true)       // Add .0 to the end of integers
     .max_significant_digits(4)  // Stop after the first 4 non-zero digits
     .radix_point(',')           // Use a ',' instead of a '.'
     .round();                   // Round after removing non-significant digits

 assert_eq!(dtoa(12459000.0, config), "12460000,0");

See the tests in src/lib.rs for examples of each feature, and the documentation to see all configurable features.

Dependencies

~74KB