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

"%.1f" of 999.99 => "999.0" #2

Closed
NicholasWMRitchie opened this issue Sep 16, 2022 · 3 comments · Fixed by #3
Closed

"%.1f" of 999.99 => "999.0" #2

NicholasWMRitchie opened this issue Sep 16, 2022 · 3 comments · Fixed by #3

Comments

@NicholasWMRitchie
Copy link

Incorrect rounding of floating point numbers:

sprintf!("%.1f",999.99).unwrap() == "999.0"

should be "1000.0"

@NicholasWMRitchie
Copy link
Author

I can't run the tests on Windows but..
I'd add tests for %f and %g:

check_fmt("%.1f",9.99);
check_fmt("%.1e",9.99);

I suspect the problem is the use of n.trunc() rather than n.round() on lines 314 & 332.

@tjol
Copy link
Owner

tjol commented Sep 17, 2022

Well spotted! I don't think just replacing trunc with round will work (that'll probably turn 1.5 into 1.5 or something), but you're on the right track

@NicholasWMRitchie
Copy link
Author

Of course, you are correct. round(...) replacing trunc(...) won't fix it.
Maybe, rounding the number to the correct number of decimal places above trunc(...) might.

@tjol tjol closed this as completed in #3 Sep 23, 2022
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

Successfully merging a pull request may close this issue.

2 participants