core: Fix to_str_exact for floats with no decimal component

This commit is contained in:
Brian Anderson 2012-02-21 14:25:31 -08:00
parent 910a32c7c7
commit 6527fc3925
2 changed files with 8 additions and 1 deletions

View File

@ -52,7 +52,7 @@ fn to_str_common(num: float, digits: uint, exact: bool) -> str {
let trunc = num as uint;
let frac = num - (trunc as float);
accum += uint::str(trunc);
if frac < epsilon || digits == 0u { ret accum; }
if (frac < epsilon && !exact) || digits == 0u { ret accum; }
accum += ".";
let i = digits;
let epsilon = 1. / pow_uint_to_uint_as_float(10u, i);
@ -83,6 +83,12 @@ fn to_str_exact(num: float, digits: uint) -> str {
to_str_common(num, digits, true)
}
#[test]
fn test_to_str_exact_do_decimal() {
let s = to_str_exact(5.0, 4u);
assert s == "5.0000";
}
/*
Function: to_str

View File

@ -136,6 +136,7 @@ fn part4() {
test(#fmt["%.5t", 3u], "00011");
test(#fmt["%.5c", 'A'], "A");
test(#fmt["%.5f", 5.82], "5.82000");
test(#fmt["%.5f", 5.0], "5.00000");
// Bool precision. I'm not sure if it's good or bad to have bool
// conversions support precision - it's not standard printf so we
// can do whatever. For now I'm making it behave the same as string