Skip to content

Commit 0647635

Browse files
committed
round to even
1 parent 33761b0 commit 0647635

File tree

2 files changed

+220
-224
lines changed

2 files changed

+220
-224
lines changed

library/core/src/fmt/num.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -330,20 +330,19 @@ macro_rules! impl_Exp {
330330
if subtracted_precision != 0 {
331331
let rem = n % 10;
332332
n /= 10;
333+
let last_digit_odd = n % 2 != 0;
333334
exponent += 1;
334-
// round up last digit
335-
if rem >= 5 {
335+
// round up last digit, round to even on a tie
336+
if rem > 5 || (rem == 5 && last_digit_odd) {
336337
n += 1;
337338
// if the digit is rounded to the next power
338339
// instead adjust the exponent
339-
if n % 10 == 0 {
340+
if n.ilog10() > (n - 1).ilog10() {
340341
n /= 10;
341342
exponent += 1;
342343
}
343344
}
344-
// n = 100
345345
}
346-
// assert!(n == 666, "{}\n{}\n{}\n",n, exponent, added_precision);
347346
(n, exponent, exponent, added_precision)
348347
};
349348

0 commit comments

Comments
 (0)