diff options
author | Victor Zverovich <victor.zverovich@gmail.com> | 2019-12-02 11:36:33 -0800 |
---|---|---|
committer | Victor Zverovich <victor.zverovich@gmail.com> | 2019-12-02 11:36:33 -0800 |
commit | 8bbe76af3a5427597185ad6207ee6e588d2eb0fd (patch) | |
tree | 9fdaac00f0ce616fb64beda5be4f37d4759f8c16 | |
parent | 4ca6821e8f26c96f815064f77dff0a74f562bffb (diff) | |
download | fmtlib-8bbe76af3a5427597185ad6207ee6e588d2eb0fd.tar.gz |
Add a missing decimal point in exponent notation with trailing zeros
-rw-r--r-- | include/fmt/format.h | 7 | ||||
-rw-r--r-- | test/format-test.cc | 1 |
2 files changed, 5 insertions, 3 deletions
diff --git a/include/fmt/format.h b/include/fmt/format.h index 398c2a77..600b0eba 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1145,10 +1145,11 @@ template <typename Char> class float_writer { if (specs_.format == float_format::exp) { // Insert a decimal point after the first digit and add an exponent. *it++ = static_cast<Char>(*digits_); - if (num_digits_ > 1) *it++ = decimal_point_; - it = copy_str<Char>(digits_ + 1, digits_ + num_digits_, it); int num_zeros = specs_.precision - num_digits_; - if (num_zeros > 0 && specs_.trailing_zeros) + bool trailing_zeros = num_zeros > 0 && specs_.trailing_zeros; + if (num_digits_ > 1 || trailing_zeros) *it++ = decimal_point_; + it = copy_str<Char>(digits_ + 1, digits_ + num_digits_, it); + if (trailing_zeros) it = std::fill_n(it, num_zeros, static_cast<Char>('0')); *it++ = static_cast<Char>(specs_.upper ? 'E' : 'e'); return write_exponent<Char>(full_exp - 1, it); diff --git a/test/format-test.cc b/test/format-test.cc index 84c9d8a0..f52e1275 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1204,6 +1204,7 @@ TEST(FormatterTest, Precision) { 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(" 0.0e+00", format("{:9.1e}", 0.0)); EXPECT_EQ( "4.9406564584124654417656879286822137236505980261432476442558568250067550" "727020875186529983636163599237979656469544571773092665671035593979639877" |