Skip to content

Commit

Permalink
round to even
Browse files Browse the repository at this point in the history
  • Loading branch information
mj10021 committed Nov 9, 2023
1 parent 33761b0 commit 0647635
Show file tree
Hide file tree
Showing 2 changed files with 220 additions and 224 deletions.
9 changes: 4 additions & 5 deletions library/core/src/fmt/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,20 +330,19 @@ macro_rules! impl_Exp {
if subtracted_precision != 0 {
let rem = n % 10;
n /= 10;
let last_digit_odd = n % 2 != 0;
exponent += 1;
// round up last digit
if rem >= 5 {
// round up last digit, round to even on a tie
if rem > 5 || (rem == 5 && last_digit_odd) {
n += 1;
// if the digit is rounded to the next power
// instead adjust the exponent
if n % 10 == 0 {
if n.ilog10() > (n - 1).ilog10() {
n /= 10;
exponent += 1;
}
}
// n = 100
}
// assert!(n == 666, "{}\n{}\n{}\n",n, exponent, added_precision);
(n, exponent, exponent, added_precision)
};

Expand Down
Loading

0 comments on commit 0647635

Please sign in to comment.