Remove trailing zeros

This commit is contained in:
Victor Zverovich 2019-04-27 07:42:27 -07:00
parent cb46397dfb
commit 40a7975640
2 changed files with 3 additions and 0 deletions

View File

@ -1213,6 +1213,8 @@ It grisu2_prettify(const char* digits, int size, int exp, It it,
if (params.num_digits >= 0 && params.num_digits < num_zeros) if (params.num_digits >= 0 && params.num_digits < num_zeros)
num_zeros = params.num_digits; num_zeros = params.num_digits;
it = std::fill_n(it, num_zeros, static_cast<Char>('0')); 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); it = copy_str<Char>(digits, digits + size, it);
} }
return it; return it;

View File

@ -1488,6 +1488,7 @@ TEST(FormatterTest, PrecisionRounding) {
EXPECT_EQ("0.002", format("{:.3f}", 0.0015)); EXPECT_EQ("0.002", format("{:.3f}", 0.0015));
EXPECT_EQ("1.000", format("{:.3f}", 0.9999)); EXPECT_EQ("1.000", format("{:.3f}", 0.9999));
EXPECT_EQ("0.00123", format("{:.3}", 0.00123)); EXPECT_EQ("0.00123", format("{:.3}", 0.00123));
EXPECT_EQ("0.1", format("{:.16g}", 0.1));
// Trigger rounding error in Grisu by a carefully chosen number. // Trigger rounding error in Grisu by a carefully chosen number.
auto n = 3788512123356.985352; auto n = 3788512123356.985352;
char buffer[64]; char buffer[64];