Skip to content

Commit 4e4c972

Browse files
committed
round to even
1 parent 33761b0 commit 4e4c972

File tree

2 files changed

+219
-224
lines changed

2 files changed

+219
-224
lines changed

library/core/src/fmt/num.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,19 +331,17 @@ macro_rules! impl_Exp {
331331
let rem = n % 10;
332332
n /= 10;
333333
exponent += 1;
334-
// round up last digit
335-
if rem >= 5 {
334+
// round up last digit, round to even on a tie
335+
if rem > 5 || (rem == 5 && n % 2 != 0) {
336336
n += 1;
337337
// if the digit is rounded to the next power
338338
// instead adjust the exponent
339-
if n % 10 == 0 {
339+
if n.ilog10() > (n - 1).ilog10() {
340340
n /= 10;
341341
exponent += 1;
342342
}
343343
}
344-
// n = 100
345344
}
346-
// assert!(n == 666, "{}\n{}\n{}\n",n, exponent, added_precision);
347345
(n, exponent, exponent, added_precision)
348346
};
349347

0 commit comments

Comments
 (0)