Don't emit trailing zeros in exponential notation (#1376)

This commit is contained in:
Victor Zverovich
2019-10-28 12:31:00 -07:00
parent b7a157401e
commit 40414b3446
3 changed files with 11 additions and 7 deletions
+9 -1
View File
@@ -1070,7 +1070,15 @@ bool grisu_format(Double value, buffer<char>& buf, int precision,
fixed_handler handler{buf.data(), 0, precision, -cached_exp10, fixed};
if (grisu_gen_digits(normalized, 1, exp, handler) == digits::error)
return false;
buf.resize(to_unsigned(handler.size));
int num_digits = handler.size;
if (!fixed) {
// Remove trailing zeros.
while (num_digits > 0 && buf[num_digits - 1] == '0') {
--num_digits;
++exp;
}
}
buf.resize(to_unsigned(num_digits));
} else {
fp fp_value;
fp lower, upper; // w^- and w^+ in the Grisu paper.
-4
View File
@@ -1073,10 +1073,6 @@ template <typename Char> class grisu_writer {
*it++ = static_cast<Char>(*digits_);
if (num_digits_ > 1) *it++ = decimal_point_;
it = copy_str<Char>(digits_ + 1, digits_ + num_digits_, it);
if (num_digits_ < params_.num_digits) {
it = std::fill_n(it, params_.num_digits - num_digits_,
static_cast<Char>('0'));
}
*it++ = static_cast<Char>(params_.upper ? 'E' : 'e');
return write_exponent<Char>(full_exp - 1, it);
}