svl/qa/unit/svl.cxx            |   20 ++++++++++++++++++++
 svl/source/numbers/zformat.cxx |    2 ++
 2 files changed, 22 insertions(+)

New commits:
commit b250f480831b4726801868ae203517938cd1c704
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Thu Mar 21 22:46:26 2024 +0500
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Fri Mar 22 20:16:07 2024 +0100

    tdf#160306: make sure that SvNumberFormatter agrees with ROUND output
    
    After commit 9eb9083ff2fdaeb96399a0830a4394de4e29ef64 (Use Dragonbox
    to implement doubleTo*String*, 2022-02-18), the rounding that is used
    in SvNumberFormatter became strictly more correct; however, it now
    differed from what ROUND spreadsheet function returned, because the
    latter uses rtl_math_round, which calls rtl::math::approxFloor.
    
    To make the visual number representation consistent, this change uses
    rtl_math_round in SvNumberformat::ImpGetNumberOutput.
    
    Change-Id: I05b0bed7d3a6c73584a77adbae2835c95be249fa
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/165142
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>
    (cherry picked from commit 805dd6bee49164d9a77de4ea9e0d53b416daca7a)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/165124
    Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org>
    Signed-off-by: Xisco Fauli <xiscofa...@libreoffice.org>
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/165195

diff --git a/svl/qa/unit/svl.cxx b/svl/qa/unit/svl.cxx
index 05b5155054d9..6fe0ebe78b82 100644
--- a/svl/qa/unit/svl.cxx
+++ b/svl/qa/unit/svl.cxx
@@ -1985,6 +1985,26 @@ CPPUNIT_TEST_FIXTURE(Test, testLanguageNone)
     CPPUNIT_ASSERT_EQUAL(OUString("dd.mm.yyyy"), 
pFormat->GetMappedFormatstring(keywords, ldw));
 }
 
+CPPUNIT_TEST_FIXTURE(Test, testTdf160306)
+{
+    // Check some cases, where the output of ROUND and of number formatter 
differed
+    SvNumberFormatter aFormatter(m_xContext, LANGUAGE_ENGLISH_US);
+    sal_uInt32 format = aFormatter.GetEntryKey(u"0.00", LANGUAGE_ENGLISH_US);
+    CPPUNIT_ASSERT(format != NUMBERFORMAT_ENTRY_NOT_FOUND);
+    OUString output;
+    const Color* color;
+    aFormatter.GetOutputString(2697.0649999999996, format, output, &color);
+    // Without the fix in place, this would fail with
+    // - Expected: 2697.07
+    // - Actual  : 2697.06
+    CPPUNIT_ASSERT_EQUAL(OUString("2697.07"), output);
+    aFormatter.GetOutputString(57.374999999999993, format, output, &color);
+    // Without the fix in place, this would fail with
+    // - Expected: 57.38
+    // - Actual  : 57.37
+    CPPUNIT_ASSERT_EQUAL(OUString("57.38"), output);
+}
+
 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
 
 }
diff --git a/svl/source/numbers/zformat.cxx b/svl/source/numbers/zformat.cxx
index 5128c5cca118..3ed57954d7e3 100644
--- a/svl/source/numbers/zformat.cxx
+++ b/svl/source/numbers/zformat.cxx
@@ -4405,6 +4405,8 @@ bool SvNumberformat::ImpGetNumberOutput(double fNumber,
         {
             nPrecExp = 0;
         }
+        // Make sure that Calc's ROUND and formatted output agree
+        fNumber = rtl_math_round(fNumber, rInfo.nCntPost, 
rtl_math_RoundingMode_Corrected);
         if (rInfo.nCntPost) // Decimal places
         {
             if ((rInfo.nCntPost + nPrecExp) > 15 && nPrecExp < 15)

Reply via email to