diff --git a/include/boost/decimal/charconv.hpp b/include/boost/decimal/charconv.hpp index 5fe93836f..cc17272ce 100644 --- a/include/boost/decimal/charconv.hpp +++ b/include/boost/decimal/charconv.hpp @@ -535,7 +535,7 @@ BOOST_DECIMAL_CONSTEXPR auto to_chars_fixed_impl(char* first, char* last, const if (num_dig == precision + 1) { --num_dig; - exponent += fenv_round(significand); + exponent += fenv_round(significand); } } else if (num_dig < precision && fmt != chars_format::general) @@ -602,7 +602,7 @@ BOOST_DECIMAL_CONSTEXPR auto to_chars_fixed_impl(char* first, char* last, const } // Bounds check again - if (precision == 0) + if (precision == 0 && !append_trailing_zeros && !append_leading_zeros) { return {r.ptr, std::errc()}; } @@ -691,6 +691,11 @@ BOOST_DECIMAL_CONSTEXPR auto to_chars_fixed_impl(char* first, char* last, const boost::decimal::detail::memset(r.ptr, '0', zeros_inserted); r.ptr += zeros_inserted; + + if (*(r.ptr - 1) == '.') + { + --r.ptr; + } } return {r.ptr, std::errc()}; diff --git a/test/test_to_chars.cpp b/test/test_to_chars.cpp index c9b2ee4e3..88594ccf0 100644 --- a/test/test_to_chars.cpp +++ b/test/test_to_chars.cpp @@ -750,6 +750,18 @@ void test_434_hex() test_value(test_one_and_quarter, "7.d0000000000000000000000000000000000000000000000000p-01", chars_format::hex, 50); } +template +void test_777() +{ + constexpr T value1 = T {21, 6, true}; + constexpr T value2 = T {211, 6, true}; + constexpr T value3 = T {2111, 6, true}; + + test_value(value1, "-21000000", chars_format::fixed, 0); + test_value(value2, "-211000000", chars_format::fixed, 0); + test_value(value3, "-2111000000", chars_format::fixed, 0); +} + int main() { test_non_finite_values(); @@ -853,6 +865,13 @@ int main() test_general_format_std(); #endif + test_777(); + test_777(); + test_777(); + test_777(); + test_777(); + test_777(); + return boost::report_errors(); }