Fix handling of missing fraction in snprintf_float

This commit is contained in:
Victor Zverovich 2019-11-29 06:17:29 -08:00
parent bb205d940d
commit b994a0ab13
2 changed files with 5 additions and 4 deletions

View File

@ -1198,17 +1198,17 @@ int snprintf_float(T value, int precision, float_specs specs,
exp = exp * 10 + (*p++ - '0');
} while (p != end);
if (sign == '-') exp = -exp;
int fraction_size = 0;
if (exp_pos != begin + 1) {
// Remove trailing zeros.
auto fraction_end = exp_pos - 1;
while (*fraction_end == '0') --fraction_end;
// Move the fractional part left to get rid of the decimal point.
int fraction_size = static_cast<int>(fraction_end - begin - 1);
fraction_size = static_cast<int>(fraction_end - begin - 1);
std::memmove(begin + 1, begin + 2, fraction_size);
buf.resize(fraction_size + offset + 1);
exp -= fraction_size;
}
return exp;
buf.resize(fraction_size + offset + 1);
return exp - fraction_size;
}
}
} // namespace internal

View File

@ -1203,6 +1203,7 @@ TEST(FormatterTest, Precision) {
EXPECT_EQ("1.2", format("{0:.2}", 1.2345));
EXPECT_EQ("1.2", format("{0:.2}", 1.2345l));
EXPECT_EQ("1.2e+56", format("{:.2}", 1.234e56));
EXPECT_EQ("1e+00", format("{:.0e}", 1.0L));
EXPECT_EQ(
"4.9406564584124654417656879286822137236505980261432476442558568250067550"
"727020875186529983636163599237979656469544571773092665671035593979639877"