diff --git a/include/etl/private/to_string_helper.h b/include/etl/private/to_string_helper.h index 76595dfc..0aa8013d 100644 --- a/include/etl/private/to_string_helper.h +++ b/include/etl/private/to_string_helper.h @@ -201,16 +201,17 @@ namespace etl void add_integral_fractional(const int64_t integral, const int64_t fractional, TIString& str, - const etl::basic_format_spec& format) + const etl::basic_format_spec& integral_format, + const etl::basic_format_spec& fractional_format) { typedef typename TIString::value_type type; - etl::private_to_string::add_integral(integral, str, format, true); + etl::private_to_string::add_integral(integral, str, integral_format, true); - if (format.get_precision() > 0) + if (fractional_format.get_precision() > 0) { str.push_back(type('.')); - etl::private_to_string::add_integral(fractional, str, format, true); + etl::private_to_string::add_integral(fractional, str, fractional_format, true); } } @@ -224,6 +225,7 @@ namespace etl const bool append) { typedef typename TIString::iterator iterator; + typedef typename TIString::value_type type; if (!append) { @@ -241,12 +243,15 @@ namespace etl // Make sure we format the two halves correctly. uint32_t max_precision = std::numeric_limits::digits10; - etl::basic_format_spec local_format = format; - local_format.decimal().width(0).precision(format.get_precision() > max_precision ? max_precision : format.get_precision()); + etl::basic_format_spec integral_format = format; + integral_format.decimal().width(0).precision(format.get_precision() > max_precision ? max_precision : format.get_precision()); + + etl::basic_format_spec fractional_format = integral_format; + fractional_format.width(integral_format.get_precision()).fill(type('0')).right(); int64_t multiplier = 1; - for (uint32_t i = 0; i < local_format.get_precision(); ++i) + for (uint32_t i = 0; i < fractional_format.get_precision(); ++i) { multiplier *= 10U; } @@ -255,7 +260,7 @@ namespace etl int64_t integral = static_cast(f_integral); int64_t fractional = static_cast(round((value - f_integral) * multiplier)); - etl::private_to_string::add_integral_fractional(integral, fractional, str, local_format); + etl::private_to_string::add_integral_fractional(integral, fractional, str, integral_format, fractional_format); } etl::private_to_string::add_alignment(str, start, format); diff --git a/include/etl/version.h b/include/etl/version.h index e59e8aed..b59b7ac7 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -39,7 +39,7 @@ SOFTWARE. #define ETL_VERSION_MAJOR 14 #define ETL_VERSION_MINOR 18 -#define ETL_VERSION_PATCH 2 +#define ETL_VERSION_PATCH 4 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) ETL_STRINGIFY(ETL_VERSION_MINOR) ETL_STRINGIFY(ETL_VERSION_PATCH) #define ETL_VERSION_W ETL_WIDE_STRING(ETL_CONCAT(ETL_CONCAT(ETL_VERSION_MAJOR, ETL_VERSION_MINOR), ETL_VERSION_PATCH)) diff --git a/support/Release notes.txt b/support/Release notes.txt index 76ca162b..0b2373fc 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,7 @@ +=============================================================================== +14.18.4 +Fixed bug in fractional part for floating point with leading zeros after the decimal point. + =============================================================================== 14.18.3 Unified 'to string' API. diff --git a/test/test_to_string.cpp b/test/test_to_string.cpp index a32bb6da..79a73e2c 100644 --- a/test/test_to_string.cpp +++ b/test/test_to_string.cpp @@ -259,6 +259,18 @@ namespace { etl::string<20> str; + CHECK_EQUAL(etl::string<20>(STR(" 0.000000")), etl::to_string(0.0, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::string<20>(STR("0.000000 ")), etl::to_string(0.0, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::string<20>(STR(" 0.000001")), etl::to_string(0.000001, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::string<20>(STR("0.000001 ")), etl::to_string(0.000001, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::string<20>(STR(" 1.000000")), etl::to_string(1.0, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::string<20>(STR("1.000000 ")), etl::to_string(1.0, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::string<20>(STR(" 1.000001")), etl::to_string(1.000001, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::string<20>(STR("1.000001 ")), etl::to_string(1.000001, str, Format().precision(6).width(10).left())); + CHECK_EQUAL(etl::string<20>(STR(" 12.345678")), etl::to_string(12.345678, str, Format().precision(6).width(10).right())); CHECK_EQUAL(etl::string<20>(STR("12.345678 ")), etl::to_string(12.345678, str, Format().precision(6).width(10).left())); } diff --git a/test/test_to_u16string.cpp b/test/test_to_u16string.cpp index 8b0b50a4..f1aea7da 100644 --- a/test/test_to_u16string.cpp +++ b/test/test_to_u16string.cpp @@ -253,6 +253,18 @@ namespace { etl::u16string<20> str; + CHECK_EQUAL(etl::u16string<20>(STR(" 0.000000")), etl::to_string(0.0, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::u16string<20>(STR("0.000000 ")), etl::to_string(0.0, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::u16string<20>(STR(" 0.000001")), etl::to_string(0.000001, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::u16string<20>(STR("0.000001 ")), etl::to_string(0.000001, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::u16string<20>(STR(" 1.000000")), etl::to_string(1.0, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::u16string<20>(STR("1.000000 ")), etl::to_string(1.0, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::u16string<20>(STR(" 1.000001")), etl::to_string(1.000001, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::u16string<20>(STR("1.000001 ")), etl::to_string(1.000001, str, Format().precision(6).width(10).left())); + CHECK_EQUAL(etl::u16string<20>(STR(" 12.345678")), etl::to_string(12.345678, str, Format().precision(6).width(10).right())); CHECK_EQUAL(etl::u16string<20>(STR("12.345678 ")), etl::to_string(12.345678, str, Format().precision(6).width(10).left())); } diff --git a/test/test_to_u32string.cpp b/test/test_to_u32string.cpp index 23b80f9d..4e39fa41 100644 --- a/test/test_to_u32string.cpp +++ b/test/test_to_u32string.cpp @@ -256,6 +256,18 @@ namespace { etl::u32string<20> str; + CHECK_EQUAL(etl::u32string<20>(STR(" 0.000000")), etl::to_string(0.0, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::u32string<20>(STR("0.000000 ")), etl::to_string(0.0, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::u32string<20>(STR(" 0.000001")), etl::to_string(0.000001, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::u32string<20>(STR("0.000001 ")), etl::to_string(0.000001, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::u32string<20>(STR(" 1.000000")), etl::to_string(1.0, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::u32string<20>(STR("1.000000 ")), etl::to_string(1.0, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::u32string<20>(STR(" 1.000001")), etl::to_string(1.000001, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::u32string<20>(STR("1.000001 ")), etl::to_string(1.000001, str, Format().precision(6).width(10).left())); + CHECK_EQUAL(etl::u32string<20>(STR(" 12.345678")), etl::to_string(12.345678, str, Format().precision(6).width(10).right())); CHECK_EQUAL(etl::u32string<20>(STR("12.345678 ")), etl::to_string(12.345678, str, Format().precision(6).width(10).left())); } diff --git a/test/test_to_wstring.cpp b/test/test_to_wstring.cpp index 6365236b..bc575ce1 100644 --- a/test/test_to_wstring.cpp +++ b/test/test_to_wstring.cpp @@ -257,6 +257,18 @@ namespace { etl::wstring<20> str; + CHECK_EQUAL(etl::wstring<20>(STR(" 0.000000")), etl::to_string(0.0, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::wstring<20>(STR("0.000000 ")), etl::to_string(0.0, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::wstring<20>(STR(" 0.000001")), etl::to_string(0.000001, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::wstring<20>(STR("0.000001 ")), etl::to_string(0.000001, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::wstring<20>(STR(" 1.000000")), etl::to_string(1.0, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::wstring<20>(STR("1.000000 ")), etl::to_string(1.0, str, Format().precision(6).width(10).left())); + + CHECK_EQUAL(etl::wstring<20>(STR(" 1.000001")), etl::to_string(1.000001, str, Format().precision(6).width(10).right())); + CHECK_EQUAL(etl::wstring<20>(STR("1.000001 ")), etl::to_string(1.000001, str, Format().precision(6).width(10).left())); + CHECK_EQUAL(etl::wstring<20>(STR(" 12.345678")), etl::to_string(12.345678, str, Format().precision(6).width(10).right())); CHECK_EQUAL(etl::wstring<20>(STR("12.345678 ")), etl::to_string(12.345678, str, Format().precision(6).width(10).left())); }