Skip to content

Commit

Permalink
round to even
Browse files Browse the repository at this point in the history
  • Loading branch information
mj10021 committed Oct 27, 2023
1 parent 5b70c2e commit 1341e63
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 223 deletions.
7 changes: 3 additions & 4 deletions library/core/src/fmt/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,10 @@ macro_rules! impl_Exp {
if subtracted_precision != 0 {
let rem = n % 10;
n /= 10;
let last_digit_odd = (n % 10) % 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
Expand All @@ -341,9 +342,7 @@ macro_rules! impl_Exp {
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 1341e63

Please sign in to comment.