-
Notifications
You must be signed in to change notification settings - Fork 13.3k
#fmt handles %f incorrectly when the number needs rounding (truncates instead) #1876
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
Labels
E-easy
Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Milestone
Comments
See also #1375 for additional problems with float formatting. |
killerswan
added a commit
to killerswan/rust
that referenced
this issue
Jun 2, 2012
This seems to fix issue rust-lang#1876, and some of the superficial parts of issue rust-lang#1375. The #fmt macro and the to_str functions will round, rather than truncate, floats as strings. Other issues remain, and I wrote more code here than intended, but the following should pass now. ``` fn x() { assert "3.1416" == #fmt["%.4f", 3.14159]; assert "3" == #fmt["%.0f", 3.14159]; assert "99" == #fmt["%.0f", 98.5]; assert "7.0000" == #fmt["%.4f", 6.999999999]; assert "3.141590000" == #fmt["%.9f", 3.14159]; } ```
brson
pushed a commit
that referenced
this issue
Jun 2, 2012
This seems to fix issue #1876, and some of the superficial parts of issue #1375. The #fmt macro and the to_str functions will round, rather than truncate, floats as strings. Other issues remain, and I wrote more code here than intended, but the following should pass now. ``` fn x() { assert "3.1416" == #fmt["%.4f", 3.14159]; assert "3" == #fmt["%.0f", 3.14159]; assert "99" == #fmt["%.0f", 98.5]; assert "7.0000" == #fmt["%.4f", 6.999999999]; assert "3.141590000" == #fmt["%.9f", 3.14159]; } ```
|
Kobzol
pushed a commit
to Kobzol/rust
that referenced
this issue
Dec 30, 2024
Most files names are relatively explicit and probably don't need to be explicited. However `rt.rs` is really not clear, and I believe it clarifies the text to indicate this is runtime service implementation.
bors
pushed a commit
to rust-lang-ci/rust
that referenced
this issue
Jan 2, 2025
Most files names are relatively explicit and probably don't need to be explicited. However `rt.rs` is really not clear, and I believe it clarifies the text to indicate this is runtime service implementation.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
E-easy
Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Looks like %f always truncates instead of rounding to the desired precision. For example, this:
use std;
fn main() {
let f = 5.1;
std::io::println(#fmt("hello world %f!", f));
}
prints out "hello world 5.099999!" instead of the expected "hello world 5.100000!".
The text was updated successfully, but these errors were encountered: