Skip to content

Commit 6527fc3

Browse files
committed
core: Fix to_str_exact for floats with no decimal component
1 parent 910a32c commit 6527fc3

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/libcore/float.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn to_str_common(num: float, digits: uint, exact: bool) -> str {
5252
let trunc = num as uint;
5353
let frac = num - (trunc as float);
5454
accum += uint::str(trunc);
55-
if frac < epsilon || digits == 0u { ret accum; }
55+
if (frac < epsilon && !exact) || digits == 0u { ret accum; }
5656
accum += ".";
5757
let i = digits;
5858
let epsilon = 1. / pow_uint_to_uint_as_float(10u, i);
@@ -83,6 +83,12 @@ fn to_str_exact(num: float, digits: uint) -> str {
8383
to_str_common(num, digits, true)
8484
}
8585

86+
#[test]
87+
fn test_to_str_exact_do_decimal() {
88+
let s = to_str_exact(5.0, 4u);
89+
assert s == "5.0000";
90+
}
91+
8692
/*
8793
Function: to_str
8894

src/test/run-pass/syntax-extension-fmt.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ fn part4() {
136136
test(#fmt["%.5t", 3u], "00011");
137137
test(#fmt["%.5c", 'A'], "A");
138138
test(#fmt["%.5f", 5.82], "5.82000");
139+
test(#fmt["%.5f", 5.0], "5.00000");
139140
// Bool precision. I'm not sure if it's good or bad to have bool
140141
// conversions support precision - it's not standard printf so we
141142
// can do whatever. For now I'm making it behave the same as string

0 commit comments

Comments
 (0)