mirror of
https://github.com/boostorg/charconv.git
synced 2026-01-19 04:02:15 +00:00
Merge pull request #269 from boostorg/267
Fix all 9s rounding on the right side of the decimal point with fixed precision
This commit is contained in:
@@ -4018,9 +4018,10 @@ round_up_all_9s:
|
||||
++decimal_dot_pos;
|
||||
}
|
||||
}
|
||||
else if (decimal_exponent_normalized == 0)
|
||||
else if (decimal_exponent_normalized == 0 || remaining_digits == 1)
|
||||
{
|
||||
// For the case 0.99...9 -> 1.00...0, the rounded digit is one before the first digit written.
|
||||
// This same case applies for 0.099 -> 0.10 in the precision = 2 instance
|
||||
// Note: decimal_exponent_normalized was negative before the increment (++decimal_exponent_normalized),
|
||||
// so we already have printed "00" onto the buffer.
|
||||
// Hence, --digit_starting_pos doesn't go more than the starting position of the buffer.
|
||||
|
||||
@@ -70,3 +70,4 @@ run github_issue_166.cpp ;
|
||||
run github_issue_166_float128.cpp ;
|
||||
run github_issue_186.cpp ;
|
||||
run github_issue_212.cpp ;
|
||||
run github_issue_267.cpp ;
|
||||
|
||||
35
test/github_issue_267.cpp
Normal file
35
test/github_issue_267.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
// Copyright 2025 Matt Borland
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// https://www.boost.org/LICENSE_1_0.txt
|
||||
//
|
||||
// See: https://github.com/boostorg/charconv/issues/267
|
||||
|
||||
#include <boost/charconv.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
template <typename T>
|
||||
void test(T value, int precision, const char* correct_result)
|
||||
{
|
||||
char buffer[64] {};
|
||||
const auto r = boost::charconv::to_chars(buffer, buffer + sizeof(buffer), value, boost::charconv::chars_format::fixed, precision);
|
||||
BOOST_TEST_CSTR_EQ(buffer, correct_result);
|
||||
BOOST_TEST(r);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test(0.09, 2, "0.09");
|
||||
test(0.099, 2, "0.10");
|
||||
test(0.0999, 2, "0.10");
|
||||
test(0.09999, 2, "0.10");
|
||||
test(0.099999, 2, "0.10");
|
||||
test(0.0999999, 2, "0.10");
|
||||
test(0.09999999, 2, "0.10");
|
||||
test(0.099999999, 2, "0.10");
|
||||
test(0.0999999999, 2, "0.10");
|
||||
test(0.09999999999, 2, "0.10");
|
||||
test(0.099999999999, 2, "0.10");
|
||||
test(0.0999999999999, 2, "0.10");
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
Reference in New Issue
Block a user