diff --git a/common/eda_units.cpp b/common/eda_units.cpp index be32cfca6e..f6623be6f5 100644 --- a/common/eda_units.cpp +++ b/common/eda_units.cpp @@ -152,6 +152,13 @@ std::string EDA_UNIT_UTILS::FormatInternalUnits( const EDA_IU_SCALE& aIuScale, i { buf.pop_back(); } + + // if the value was really small + // we may have just stripped all the zeros after the decimal + if( buf[buf.size() - 1] == '.' ) + { + buf.pop_back(); + } } else { diff --git a/common/string_utils.cpp b/common/string_utils.cpp index 5b1a6d7e8f..a79b4dbbea 100644 --- a/common/string_utils.cpp +++ b/common/string_utils.cpp @@ -1104,11 +1104,18 @@ std::string FormatDouble2Str( double aValue ) { buf = fmt::format( "{:.16f}", aValue ); - // remove trailing zeros + // remove trailing zeros (and the decimal marker if needed) while( !buf.empty() && buf[buf.size() - 1] == '0' ) { buf.pop_back(); } + + // if the value was really small + // we may have just stripped all the zeros after the decimal + if( buf[buf.size() - 1] == '.' ) + { + buf.pop_back(); + } } else { @@ -1133,7 +1140,7 @@ std::string UIDouble2Str( double aValue ) while( --len > 0 && buf[len] == '0' ) buf[len] = '\0'; - if( buf[len] == '.' ) + if( buf[len] == '.' || buf[len] == ',' ) buf[len] = '\0'; else ++len; diff --git a/qa/unittests/common/test_kicad_string.cpp b/qa/unittests/common/test_kicad_string.cpp index 045c20a1b8..148bb37750 100644 --- a/qa/unittests/common/test_kicad_string.cpp +++ b/qa/unittests/common/test_kicad_string.cpp @@ -36,6 +36,7 @@ */ BOOST_AUTO_TEST_SUITE( KicadString ) + /** * Test the #GetTrailingInt method. */ @@ -58,6 +59,7 @@ BOOST_AUTO_TEST_CASE( TrailingInt ) } } + /** * Test the #StrNumCmp method. */ @@ -113,4 +115,30 @@ BOOST_AUTO_TEST_CASE( NaturalNumberCompare ) } } -BOOST_AUTO_TEST_SUITE_END() + +/** + * Test the #GetTrailingInt method. + */ +BOOST_AUTO_TEST_CASE( Double2Str ) +{ + using CASE = std::pair; + + // conceptually a little quirky because doubles do have all those pesky additional values + const std::vector cases = { + { 0, "0" }, + { 1.000, "1" }, + { 1.050, "1.05" }, // single trailing zero + { 0.00001523, "0.00001523" }, // value less than the magic 0.0001 threshold + { 0.00000000000000001523, "0" }, // really small decimal that gets cut off + { 623523, "623523" }, // large whole number + }; + + for( const auto& c : cases ) + { + // Test both of these functions that work the same but the innards are different + BOOST_CHECK_EQUAL( FormatDouble2Str( c.first ), c.second ); + BOOST_CHECK_EQUAL( UIDouble2Str( c.first ), c.second ); + } +} + +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file