From 172d5649106d8602ee258b7002b78459edd4855c Mon Sep 17 00:00:00 2001 From: Eike Rathke <erack@redhat.com> Date: Sun, 23 May 2021 13:52:05 +0200 Subject: Resolves: tdf#137063 For General format start exponential display at 1E-10 ... instead of 1E-05, if not too many significant digits. Change-Id: I66cca1bdd2742526e4ade08a691e6d43b2eb439e Reviewed-on: https://gerrit.libreoffice.org/c/core/+/116016 Reviewed-by: Eike Rathke <erack@redhat.com> Tested-by: Jenkins --- svl/source/numbers/zformat.cxx | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) (limited to 'svl') diff --git a/svl/source/numbers/zformat.cxx b/svl/source/numbers/zformat.cxx index 648034f9352a..19ee2134df35 100644 --- a/svl/source/numbers/zformat.cxx +++ b/svl/source/numbers/zformat.cxx @@ -2489,18 +2489,33 @@ bool SvNumberformat::GetOutputString(double fNumber, { OutString = "0"; } - else if (fNumber < EXP_LOWER_BOUND && fNumber > -EXP_LOWER_BOUND) - { - OutString = ::rtl::math::doubleToUString( fNumber, - rtl_math_StringFormat_E2, - 15, - GetFormatter().GetNumDecimalSep()[0], true); - } else if (fNumber < 1.0 && fNumber > -1.0) { - OutString = ::rtl::math::doubleToUString( fNumber, - rtl_math_StringFormat_Automatic, - 15, + // Decide whether to display as 0.000000123... or 1.23...e-07 + bool bFix = (fNumber < -EXP_LOWER_BOUND || EXP_LOWER_BOUND < fNumber); + if (!bFix) + { + // Arbitrary, not too many 0s visually, start E2 at 1E-10. + constexpr sal_Int32 kMaxExp = 9; + const sal_Int32 nExp = static_cast<sal_Int32>(ceil( -log10( fabs( fNumber)))); + if (nExp <= kMaxExp && rtl::math::approxEqual( + rtl::math::round( fNumber, 16), rtl::math::round( fNumber, nExp + 16))) + { + // Not too many significant digits or accuracy + // artefacts, otherwise leave everything to E2 + // format. + bFix = true; + } + } + if (bFix) + OutString = ::rtl::math::doubleToUString( fNumber, + rtl_math_StringFormat_F, + rtl_math_DecimalPlaces_Max, + GetFormatter().GetNumDecimalSep()[0], true); + else + OutString = ::rtl::math::doubleToUString( fNumber, + rtl_math_StringFormat_E2, + rtl_math_DecimalPlaces_Max, GetFormatter().GetNumDecimalSep()[0], true); } else -- cgit