Don't remove trailing zeros with #

This commit is contained in:
Victor Zverovich 2020-08-08 08:14:39 -07:00
parent e06ae32294
commit 4fd95e4b4d
2 changed files with 2 additions and 1 deletions

View File

@ -1131,7 +1131,7 @@ int format_float(T value, int precision, float_specs specs, buffer<char>& buf) {
if (grisu_gen_digits(normalized, 1, exp, handler) == digits::error) if (grisu_gen_digits(normalized, 1, exp, handler) == digits::error)
return snprintf_float(value, precision, specs, buf); return snprintf_float(value, precision, specs, buf);
int num_digits = handler.size; int num_digits = handler.size;
if (!fixed) { if (!fixed && !specs.showpoint) {
// Remove trailing zeros. // Remove trailing zeros.
while (num_digits > 0 && buf[num_digits - 1] == '0') { while (num_digits > 0 && buf[num_digits - 1] == '0') {
--num_digits; --num_digits;

View File

@ -761,6 +761,7 @@ TEST(FormatterTest, HashFlag) {
EXPECT_EQ("-42.0", format("{0:#}", -42.0l)); EXPECT_EQ("-42.0", format("{0:#}", -42.0l));
EXPECT_EQ("4.e+01", format("{:#.0e}", 42.0)); EXPECT_EQ("4.e+01", format("{:#.0e}", 42.0));
EXPECT_EQ("0.", format("{:#.0f}", 0.01)); EXPECT_EQ("0.", format("{:#.0f}", 0.01));
EXPECT_EQ("0.50", format("{:#.2g}", 0.5));
auto s = format("{:#.0f}", 0.5); // MSVC's printf uses wrong rounding mode. auto s = format("{:#.0f}", 0.5); // MSVC's printf uses wrong rounding mode.
EXPECT_TRUE(s == "0." || s == "1."); EXPECT_TRUE(s == "0." || s == "1.");
EXPECT_THROW_MSG(format("{0:#", 'c'), format_error, EXPECT_THROW_MSG(format("{0:#", 'c'), format_error,