-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Float rounding with specified digits #3608
Comments
you can get a library function into std faster through the ACP process https://std-dev-guide.rust-lang.org/development/feature-lifecycle.html#suitability-for-the-standard-library. anyway what is the use case for this not covered by |
@kennytm |
I want to have float in the output. If I use format! macro then I have to use parse and then it becomes verbose. I'm currently translating a Python repo to Rust and I need to preserve original functionality to avoid breaking changes. The original Python repo uses an operation that rounds off to 2 digits and so I need to do the same in the Rust version of the repo |
if you need to be interoperate with Python do note that Rust's rounding result might not be the same as that of Python.
And CPython's |
Introduce method
round_to_digits(T: u32)
forf64
(andf32
). Same asf64::round()
except that you can specify how many digits you want to round to.Example use case:
I have the number
12.34567
and I want to round it to12.35
. Currentf64::round()
requires a verbose/less-readable/bug-prone workaround:The proposed method
round_to()
would simplify the above to:The text was updated successfully, but these errors were encountered: