Skip to content

#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

Closed
bzbarsky opened this issue Feb 21, 2012 · 2 comments
Closed
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@bzbarsky
Copy link

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!".

@brson
Copy link
Contributor

brson commented Feb 21, 2012

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];
}
```
@brson
Copy link
Contributor

brson commented Jun 2, 2012

float::to_str rounding is fixed by @killerswan in a7359f5. The default precision is still an open issue #1375

@brson brson closed this as completed Jun 2, 2012
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.
Projects
None yet
Development

No branches or pull requests

2 participants