Skip to content

Commit

Permalink
Fix small number rounding with fixed precision in grisu
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jul 4, 2019
1 parent 6a03134 commit edd13fc
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ struct fixed_handler {
// Check if precision is satisfied just by leading zeros, e.g.
// format("{:.2f}", 0.001) gives "0.00" without generating any digits.
if (precision > 0) return digits::more;
if (precision < 0) return digits::done;
auto dir = get_round_direction(divisor, remainder, error);
if (dir == unknown) return digits::error;
buf[size++] = dir == up ? '1' : '0';
Expand Down
1 change: 1 addition & 0 deletions test/grisu-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ TEST(GrisuTest, Zero) { EXPECT_EQ("0.0", fmt::format("{}", 0.0)); }
TEST(GrisuTest, Round) {
EXPECT_EQ("1.9156918820264798e-56",
fmt::format("{}", 1.9156918820264798e-56));
EXPECT_EQ("0.0000", fmt::format("{:.4f}", 7.2809479766055470e-15));
}

TEST(GrisuTest, Prettify) {
Expand Down

0 comments on commit edd13fc

Please sign in to comment.