bcmyers / num-format

A Rust crate for producing string representations of numbers, formatted according to international standards
Apache License 2.0
122 stars 22 forks source link

Always show 2 decimals #24

Open kpcyrd opened 3 years ago

kpcyrd commented 3 years ago

Hello!

I'm trying to format some numbers like this:

1337   -> 1337.00
123.4  -> 123.40
123.45 -> 123.45

In the rust standard library this would work with {:.2}, I couldn't figure out how to do this with num_format though.

Pointers much appreciated!

GoldenChrysus commented 3 months ago

I ran into this today (formatting currency) but it looks like this crate doesn't even support floats. The implementations are on (un)signed ints and bigints. The crate has mechanisms for indicating the decimal point character for a locale but doesn't do anything with them much less allow for fixed precision.

I made a fork to implement for rust_decimal::Decimal that may be useful if using the rust_decimal crate but probably not useful if using floats. Though that fork doesn't provide any fixed-width formatting API; rather you'd rescale your Decimal to the required precision/scale that way it already has the correct number of decimal places when formatting.

It looks like the numfmt crate may be better conceptually than this crate for very accurately displaying a variety of numbers, but it does not have the locale concept so one would have to recreate all of their required locales if using that crate.