diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 80e0f8f4..5ebad700 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -1626,7 +1626,8 @@ struct fixed_handler { } if (buf[0] > '9') { buf[0] = '1'; - buf[size++] = '0'; + if (fixed) buf[size++] = '0'; + else ++exp10; } return digits::done; } @@ -2466,7 +2467,7 @@ int format_float(T value, int precision, float_specs specs, buffer& buf) { exp += handler.size - cached_exp10 - 1; fallback_format(value, handler.precision, specs.binary32, buf, exp); } else { - exp -= cached_exp10; + exp += handler.exp10; buf.try_resize(to_unsigned(handler.size)); } if (!fixed && !specs.showpoint) { diff --git a/test/format-test.cc b/test/format-test.cc index 911d1d58..d5d3f6c9 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -957,6 +957,7 @@ TEST(FormatterTest, Precision) { EXPECT_EQ("0.001", format("{:.1g}", 0.001)); EXPECT_EQ("1019666400", format("{}", 1019666432.0f)); EXPECT_EQ("1e+01", format("{:.0e}", 9.5)); + EXPECT_EQ("1.0e-34", fmt::format("{:.1e}", 1e-34)); EXPECT_THROW_MSG(format("{0:.2}", reinterpret_cast(0xcafe)), format_error,