Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/boost/charconv/detail/dragonbox/floff.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1764,7 +1764,7 @@ BOOST_CHARCONV_SAFEBUFFERS to_chars_result floff(const double x, int precision,

const auto initial_digits = static_cast<std::uint32_t>(prod >> 32);

buffer -= (initial_digits < 10 && buffer != first ? 1 : 0);
buffer -= (initial_digits < 10 && buffer != first && precision != 0 ? 1 : 0);
remaining_digits -= (2 - (initial_digits < 10 ? 1 : 0));

// Avoid the situation where we have a leading 0 that we don't need
Expand Down
8 changes: 6 additions & 2 deletions include/boost/charconv/detail/ryu/ryu_generic_128.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,14 +436,18 @@ static inline int generic_to_chars_fixed(const struct floating_decimal_128 v, ch
{
current_len = static_cast<int>(shift_width) + precision;
}
else
{
current_len = static_cast<int>(shift_width) - 1;
}

precision = 0;
// Since we wrote additional characters into the buffer we need to add a null terminator,
// Since we wrote additional characters into the buffer, we need to add a null terminator,
// so they are not read
const auto round_val = result[current_len];
result[current_len] = '\0';

// More complicated rounding situations like 9999.999999 are already handled
// More complicated rounding situations like 9999.999999 are already handled,
// so we don't need to worry about rounding past the decimal point
if (round_val >= '5')
{
Expand Down
1 change: 1 addition & 0 deletions test/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ run github_issue_267.cpp ;
run github_issue_280.cpp ;
run github_issue_282.cpp ;
run github_issue_int128_320.cpp ;
run github_issue_296.cpp ;
67 changes: 67 additions & 0 deletions test/github_issue_296.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2026 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/296

#include <boost/charconv.hpp>
#include <boost/core/lightweight_test.hpp>

template <typename T>
void test()
{
const T value = -125.125;

char buffer[64];
const auto r = boost::charconv::to_chars(buffer, buffer + sizeof(buffer), value, boost::charconv::chars_format::fixed, 0);
BOOST_TEST(r);
*r.ptr = '\0';

BOOST_TEST_CSTR_EQ(buffer, "-125");
}

template <typename T>
void test_pos()
{
const T value = 125.125;

char buffer[64];
const auto r = boost::charconv::to_chars(buffer, buffer + sizeof(buffer), value, boost::charconv::chars_format::fixed, 0);
BOOST_TEST(r);
*r.ptr = '\0';

BOOST_TEST_CSTR_EQ(buffer, "125");
}

template <typename T>
void test_prec_1()
{
const T value = -125.125;

char buffer[64];
const auto r = boost::charconv::to_chars(buffer, buffer + sizeof(buffer), value, boost::charconv::chars_format::fixed, 1);
BOOST_TEST(r);
*r.ptr = '\0';

BOOST_TEST_CSTR_EQ(buffer, "-125.1");
}

int main()
{
test<float>();
test<double>();

test_pos<float>();
test_pos<double>();

test_prec_1<float>();
test_prec_1<double>();

#if !defined(BOOST_CHARCONV_UNSUPPORTED_LONG_DOUBLE) && BOOST_CHARCONV_LDBL_BITS < 128
test<long double>();
test_pos<long double>();
test_prec_1<long double>();
#endif

return boost::report_errors();
}
Loading