diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 5c6f1a84..0318298d 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -588,7 +588,7 @@ struct fixed_handler { } digits::result on_digit(char digit, uint64_t divisor, uint64_t remainder, - uint64_t error, int& exp, bool integral) { + uint64_t error, int, bool integral) { FMT_ASSERT(remainder < divisor, ""); buf[size++] = digit; if (size != precision) return digits::more; @@ -601,7 +601,6 @@ struct fixed_handler { FMT_ASSERT(error == 1 && divisor > 2, ""); } auto dir = get_round_direction(divisor, remainder, error); - // TODO: test rounding if (dir != up) return dir == down ? digits::done : digits::error; ++buf[size - 1]; for (int i = size - 1; i > 0 && buf[i] > '9'; --i) { @@ -610,7 +609,7 @@ struct fixed_handler { } if (buf[0] > '9') { buf[0] = '1'; - ++exp; + buf[size++] = '0'; } return digits::done; } diff --git a/test/format-test.cc b/test/format-test.cc index afaec81b..b03b8af1 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1476,7 +1476,13 @@ TEST(FormatterTest, PrecisionRounding) { EXPECT_EQ("0.001", format("{:.3f}", 0.0005)); EXPECT_EQ("0.001", format("{:.3f}", 0.00149)); EXPECT_EQ("0.002", format("{:.3f}", 0.0015)); + EXPECT_EQ("1.000", format("{:.3f}", 0.9999)); EXPECT_EQ("0.00123", format("{:.3}", 0.00123)); + // Trigger rounding error in Grisu by a carefully chosen number. + auto n = 3788512123356.985352; + char buffer[64]; + sprintf(buffer, "%f", n); + EXPECT_EQ(buffer, format("{:f}", n)); } TEST(FormatterTest, FormatNaN) {