Fix unexpected trailing decimal point (#1153)

This commit is contained in:
Alex Alabuzhev 2019-05-10 21:37:19 +01:00 committed by Victor Zverovich
parent ccc318e807
commit 77d6036cd5
2 changed files with 5 additions and 1 deletions

View File

@ -1187,12 +1187,14 @@ It grisu_prettify(const char* digits, int size, int exp, It it,
} else if (full_exp > 0) {
// 1234e-2 -> 12.34[0+]
it = copy_str<Char>(digits, digits + full_exp, it);
*it++ = static_cast<Char>('.');
if (!params.trailing_zeros) {
// Remove trailing zeros.
while (size > full_exp && digits[size - 1] == '0') --size;
if (size != full_exp)
*it++ = static_cast<Char>('.');
return copy_str<Char>(digits + full_exp, digits + size, it);
}
*it++ = static_cast<Char>('.');
it = copy_str<Char>(digits + full_exp, digits + size, it);
if (params.num_digits > size) {
// Add trailing zeros.

View File

@ -406,6 +406,8 @@ TEST(PrintfTest, Float) {
EXPECT_PRINTF(buffer, "%E", 392.65);
EXPECT_PRINTF("392.65", "%g", 392.65);
EXPECT_PRINTF("392.65", "%G", 392.65);
EXPECT_PRINTF("392", "%g", 392.0);
EXPECT_PRINTF("392", "%G", 392.0);
safe_sprintf(buffer, "%a", -392.65);
EXPECT_EQ(buffer, format("{:a}", -392.65));
safe_sprintf(buffer, "%A", -392.65);