diff --git a/src/base/AnyMap.cpp b/src/base/AnyMap.cpp index dbfdc7cfa5..b72367e99e 100644 --- a/src/base/AnyMap.cpp +++ b/src/base/AnyMap.cpp @@ -171,7 +171,15 @@ string formatDouble(double x, long int precision) string s0; if (useExp) { s0 = fmt::format(fmt::format("{:.{}e}", x, precision)); - last = s0.size() - 5; // last digit of significand + // last digit of significand + last = s0.size() - 5; + if (s0[last + 1] == 'e') { + // pass - most values use four letter exponent (examples: e+05, e-03) + } else if (s0[last] == 'e') { + last--; // exponents larger than e+99 or smaller than e-99 (example: e+100) + } else { + last = s0.find('e') - 1; // backstop; slower, but will always work + } } else { log10x = static_cast(std::floor(std::log10(std::abs(x)))); s0 = fmt::format("{:.{}f}", x, precision - log10x); diff --git a/test/general/test_serialization.cpp b/test/general/test_serialization.cpp index 30515a5c1c..cf44ba018b 100644 --- a/test/general/test_serialization.cpp +++ b/test/general/test_serialization.cpp @@ -38,6 +38,9 @@ TEST(YamlWriter, formatDouble) // check edge cases m["a"] = -1061.793215682400; EXPECT_EQ(m.toYamlString(), "a: -1061.7932156824\n"); + + m["a"] = 7.820059054328200e-111; + EXPECT_EQ(m.toYamlString(), "a: 7.8200590543282e-111\n"); } TEST(YamlWriter, formatDoubleExp)