Skip to content

Commit

Permalink
Don't emit decimal point if there are no trailing digits (#1210)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jun 30, 2019
1 parent bd3fd3b commit ab0ba8a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
8 changes: 5 additions & 3 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1096,14 +1096,16 @@ It grisu_prettify(const char* digits, int size, int exp, It it,
} else {
// 1234e-6 -> 0.001234
*it++ = static_cast<Char>('0');
*it++ = static_cast<Char>('.');
int num_zeros = -full_exp;
if (params.num_digits >= 0 && params.num_digits < num_zeros)
num_zeros = params.num_digits;
it = std::fill_n(it, num_zeros, static_cast<Char>('0'));
if (!params.trailing_zeros)
while (size > 0 && digits[size - 1] == '0') --size;
it = copy_str<Char>(digits, digits + size, it);
if (num_zeros != 0 || size != 0) {
*it++ = static_cast<Char>('.');
it = std::fill_n(it, num_zeros, static_cast<Char>('0'));
it = copy_str<Char>(digits, digits + size, it);
}
}
return it;
}
Expand Down
1 change: 1 addition & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,7 @@ TEST(FormatterTest, FormatDouble) {

TEST(FormatterTest, PrecisionRounding) {
EXPECT_EQ("0", format("{:.0f}", 0.0));
EXPECT_EQ("0", format("{:.0f}", 0.01));
EXPECT_EQ("0", format("{:.0f}", 0.1));
EXPECT_EQ("0.000", format("{:.3f}", 0.00049));
EXPECT_EQ("0.001", format("{:.3f}", 0.0005));
Expand Down

0 comments on commit ab0ba8a

Please sign in to comment.