Skip to content

Commit

Permalink
Don't print trailing zero with fixed, precision=0, and showpoint (#1417)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Nov 24, 2019
1 parent 43271ba commit 28d7191
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
14 changes: 9 additions & 5 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1136,9 +1136,14 @@ template <typename Char> class float_writer {
// 1234e7 -> 12340000000[.0+]
it = copy_str<Char>(digits_, digits_ + num_digits_, it);
it = std::fill_n(it, full_exp - num_digits_, static_cast<Char>('0'));
int num_zeros = (std::max)(params_.num_digits - full_exp, 1);
if (params_.trailing_zeros) {
*it++ = decimal_point_;
int num_zeros = params_.num_digits - full_exp;
if (num_zeros <= 0) {
if (params_.format != float_format::fixed)
*it++ = static_cast<Char>('0');
return it;
}
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
if (num_zeros > 1000)
throw std::runtime_error("fuzz mode - avoiding excessive cpu use");
Expand Down Expand Up @@ -1191,10 +1196,9 @@ template <typename Char> class float_writer {
decimal_point_(decimal_point) {
int full_exp = num_digits + exp - 1;
int precision = params.num_digits > 0 ? params.num_digits : 16;
if (params_.format == float_format::general) {
params_.format = full_exp >= -4 && full_exp < precision
? float_format::fixed
: float_format::exp;
if (params_.format == float_format::general &&
!(full_exp >= -4 && full_exp < precision)) {
params_.format = float_format::exp;
}
size_ = prettify(counting_iterator()).count();
size_ += params_.sign ? 1 : 0;
Expand Down
1 change: 1 addition & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,7 @@ TEST(FormatterTest, Precision) {
"012970999954193198940908041656332452475714786901472678015935523861155013"
"480352649347201937902681071074917033322268447533357208324319361e-324",
format("{:.494}", 4.9406564584124654E-324));
EXPECT_EQ("123.", format("{:#.0f}", 123.0));

EXPECT_THROW_MSG(format("{0:.2}", reinterpret_cast<void*>(0xcafe)),
format_error,
Expand Down

0 comments on commit 28d7191

Please sign in to comment.