Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always show 2 decimals #24

Open
kpcyrd opened this issue Jun 13, 2021 · 1 comment
Open

Always show 2 decimals #24

kpcyrd opened this issue Jun 13, 2021 · 1 comment

Comments

@kpcyrd
Copy link

kpcyrd commented Jun 13, 2021

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
Copy link

GoldenChrysus commented Jul 1, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants