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

Float rounding with specified digits #3608

Open
amab8901 opened this issue Apr 10, 2024 · 4 comments
Open

Float rounding with specified digits #3608

amab8901 opened this issue Apr 10, 2024 · 4 comments

Comments

@amab8901
Copy link

amab8901 commented Apr 10, 2024

Introduce method round_to_digits(T: u32) for f64 (and f32). Same as f64::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 to 12.35. Current f64::round() requires a verbose/less-readable/bug-prone workaround:

let full_float = 12.34567;
let rounded_float = (full_float * 100.0).round() / 100.0;
assert_eq!(rounded_float, 12.35);

The proposed method round_to() would simplify the above to:

let rounded_float = 12.34567.round_to_digits(2);
assert_eq!(rounded_float, 12.35);
@amab8901 amab8901 changed the title Float rounding with specified digits. Float rounding with specified digits Apr 10, 2024
@kennytm
Copy link
Member

kennytm commented Apr 10, 2024

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 format!("{:.2}", full_float)?

@CryZe
Copy link

CryZe commented Apr 10, 2024

@kennytm .2 always prints 2 fractional digits, even if they are 0.

@amab8901
Copy link
Author

amab8901 commented Apr 10, 2024

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 format!("{:.2}", full_float)?

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

@kennytm
Copy link
Member

kennytm commented Apr 10, 2024

if you need to be interoperate with Python do note that Rust's rounding result might not be the same as that of Python.

Number 0.05 0.15 0.25 0.35 0.45 0.55 0.65 0.75 0.85 0.95
CPython round(x, 1) 0.1 0.1 0.2 0.3 0.5 0.6 0.7 0.8 0.8 0.9
format!("{:.2}", f64) 0.1 0.1 0.2 0.3 0.5 0.6 0.7 0.8 0.8 0.9
format!("{:.2}", f32) 0.1 0.2 0.2 0.3 0.4 0.6 0.6 0.8 0.9 0.9
(x * 10.0).round() / 10.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(x*10).round_ties_even()/10 0 0.2 0.2 0.4 0.4 0.6 0.6 0.8 0.8 1

And CPython's round(x, n) (for n > 0) is implemented exactly as format!("{:.1$}", x, n).parse::<f64>() since 2.7.4 / 3.1.0 (python/cpython@e6a076d), surprise 🙃.

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

3 participants