From edd13fcc1cc4b0d3f24925118ac5cc745baea2b6 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Thu, 4 Jul 2019 06:59:11 -0700 Subject: [PATCH] Fix small number rounding with fixed precision in grisu --- include/fmt/format-inl.h | 1 + test/grisu-test.cc | 1 + 2 files changed, 2 insertions(+) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index c3a38b5f..59defa2d 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -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'; diff --git a/test/grisu-test.cc b/test/grisu-test.cc index 07c344d3..79a13191 100644 --- a/test/grisu-test.cc +++ b/test/grisu-test.cc @@ -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) {