[Libreoffice-commits] core.git: include/svl sc/qa sc/source

Laurent Balland-Poirier laurent.balland-poirier at laposte.net
Tue Mar 14 16:32:29 UTC 2017


 include/svl/zformat.hxx          |    3 -
 sc/qa/unit/ucalc.cxx             |  103 +++++++++++++++++++++++++++++----------
 sc/source/core/data/documen4.cxx |   20 ++++++-
 3 files changed, 97 insertions(+), 29 deletions(-)

New commits:
commit 5f2db66ad0de6fbbae309850516e17eaa17934cb
Author: Laurent Balland-Poirier <laurent.balland-poirier at laposte.net>
Date:   Fri Mar 3 21:51:26 2017 +0100

    tdf#106252 Engineering notation for Precision as shown
    
    with more tests
    
    Change-Id: Ifc77b847af4eaaa3c85e904e46c2663b6d768241
    Reviewed-on: https://gerrit.libreoffice.org/35089
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Eike Rathke <erack at redhat.com>

diff --git a/include/svl/zformat.hxx b/include/svl/zformat.hxx
index 85250ac..1dd05d2 100644
--- a/include/svl/zformat.hxx
+++ b/include/svl/zformat.hxx
@@ -223,7 +223,8 @@ public:
         { return NumFor[nIx].Info().nCntPost; }
 
     /// Count of integer digits
-    sal_uInt16 GetFormatIntegerDigits() const { return NumFor[0].Info().nCntPre; }
+    sal_uInt16 GetFormatIntegerDigits( sal_uInt16 nIx = 0 ) const
+        { return NumFor[nIx].Info().nCntPre; }
 
     //! Read/write access on a special sal_uInt16 component, may only be used on the
     //! standard format 0, 10000, ... and only by the number formatter!
diff --git a/sc/qa/unit/ucalc.cxx b/sc/qa/unit/ucalc.cxx
index 9a51d92..86aa877 100644
--- a/sc/qa/unit/ucalc.cxx
+++ b/sc/qa/unit/ucalc.cxx
@@ -6773,10 +6773,12 @@ void Test::testPrecisionAsShown()
         aCode = "0.00";
         fValue = 1.0/3.0;
         fExpectedRoundVal = 0.33;
-        checkPrecisionAsShown( aCode, fValue, fExpectedRoundVal );
-        fValue = -10.001;
-        fExpectedRoundVal = -10.0;
-        checkPrecisionAsShown( aCode, fValue, fExpectedRoundVal );
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
+        checkPrecisionAsShown( aCode, -fValue, -fExpectedRoundVal );
+        fValue = 10.001;
+        fExpectedRoundVal = 10.0;
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
+        checkPrecisionAsShown( aCode, -fValue, -fExpectedRoundVal );
     }
     {   // thousand rounding bogus!!!! tdf#106253
         aCode = "0,,";
@@ -6791,54 +6793,105 @@ void Test::testPrecisionAsShown()
         aCode = "0.00%";
         fValue = 4.0 / 7.0;
         fExpectedRoundVal = 0.5714;
-        checkPrecisionAsShown( aCode, fValue, fExpectedRoundVal );
-        fValue = -4.0 / 7.0;
-        fExpectedRoundVal = -0.5714;
-        checkPrecisionAsShown( aCode, fValue, fExpectedRoundVal );
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
+        checkPrecisionAsShown( aCode, -fValue, -fExpectedRoundVal );
+        fValue = 40.0 / 7.0;
+        fExpectedRoundVal = 5.7143;
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
+        checkPrecisionAsShown( aCode, -fValue, -fExpectedRoundVal );
     }
     {   // scientific rounding
         aCode = "0.00E0";
         fValue = 400000.0 / 7.0;
         fExpectedRoundVal = 57100.0;
-        checkPrecisionAsShown( aCode, fValue, fExpectedRoundVal );
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
+        checkPrecisionAsShown( aCode, -fValue, -fExpectedRoundVal );
         fValue = 4.0 / 70000.0;
         fExpectedRoundVal = 5.71e-5;
-        checkPrecisionAsShown( aCode, fValue, fExpectedRoundVal );
-        // engineering rounding bogus!!! tdf#106252
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
+        checkPrecisionAsShown( aCode, -fValue, -fExpectedRoundVal );
+        // engineering rounding tdf#106252
         aCode = "##0.000E0";
         fValue = 400000.0 / 7.0;
-        fExpectedRoundVal = 57143.0; // actual is 57140
-        //checkPrecisionAsShown( aCode, fValue, fExpectedRoundVal );
+        fExpectedRoundVal = 57.143e3;
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
+        checkPrecisionAsShown( aCode, -fValue, -fExpectedRoundVal );
+        fValue = 4000000.0 / 7.0;
+        fExpectedRoundVal = 571.429e3;
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
+        checkPrecisionAsShown( aCode, -fValue, -fExpectedRoundVal );
+        fValue = 40000000.0 / 7.0;
+        fExpectedRoundVal = 5.714e6;
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
+        checkPrecisionAsShown( aCode, -fValue, -fExpectedRoundVal );
         fValue = 4.0 / 70000.0;
-        fExpectedRoundVal = 5.7143e-5; // actual is 5.714e-05
-        //checkPrecisionAsShown( aCode, fValue, fExpectedRoundVal );
+        fExpectedRoundVal = 57.143e-6;
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
+        checkPrecisionAsShown( aCode, -fValue, -fExpectedRoundVal );
+        fValue = 4.0 / 7000.0;
+        fExpectedRoundVal = 571.429e-6;
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
+        checkPrecisionAsShown( aCode, -fValue, -fExpectedRoundVal );
+        fValue = 4.0 / 700.0;
+        fExpectedRoundVal = 5.714e-3;
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
+        checkPrecisionAsShown( aCode, -fValue, -fExpectedRoundVal );
+        aCode = "##?0.0#E0";
+        fValue = 400000.0 / 7.0;
+        fExpectedRoundVal = 5.71e4;
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
+        checkPrecisionAsShown( aCode, -fValue, -fExpectedRoundVal );
+        fValue = 4000000.0 / 7.0;
+        fExpectedRoundVal = 57.14e4;
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
+        checkPrecisionAsShown( aCode, -fValue, -fExpectedRoundVal );
+        fValue = 40000000.0 / 7.0;
+        fExpectedRoundVal = 571.43e4;
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
+        checkPrecisionAsShown( aCode, -fValue, -fExpectedRoundVal );
+        fValue = 400000000.0 / 7.0;
+        fExpectedRoundVal = 5714.29e4;
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
+        checkPrecisionAsShown( aCode, -fValue, -fExpectedRoundVal );
+        fValue = 4.0 / 70000.0;
+        fExpectedRoundVal = 5714.29e-8;
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
+        checkPrecisionAsShown( aCode, -fValue, -fExpectedRoundVal );
+        fValue = 4.0 / 7000.0;
+        fExpectedRoundVal = 5.71e-4;
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
+        checkPrecisionAsShown( aCode, -fValue, -fExpectedRoundVal );
+        fValue = 4.0 / 700.0;
+        fExpectedRoundVal = 57.14e-4;
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
+        checkPrecisionAsShown( aCode, -fValue, -fExpectedRoundVal );
+        fValue = 4.0 / 70.0;
+        fExpectedRoundVal = 571.43e-4;
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
+        checkPrecisionAsShown( aCode, -fValue, -fExpectedRoundVal );
     }
     {   // fraction rounding tdf#105657
         aCode = "# ?/?";
         fValue = 0.35;
         fExpectedRoundVal = 1.0/3.0;
-        checkPrecisionAsShown( aCode, fValue, fExpectedRoundVal );
-        fValue = -0.35;
-        fExpectedRoundVal = -1.0/3.0;
-        checkPrecisionAsShown( aCode, fValue, fExpectedRoundVal );
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
+        checkPrecisionAsShown( aCode, -fValue, -fExpectedRoundVal );
     }
     {   // exact fraction
         aCode = "# ?/??";
         fValue = 0.35;
         fExpectedRoundVal = 0.35;
-        checkPrecisionAsShown( aCode, fValue, fExpectedRoundVal );
-        fValue = -0.35;
-        fExpectedRoundVal = -0.35;
-        checkPrecisionAsShown( aCode, fValue, fExpectedRoundVal );
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
+        checkPrecisionAsShown( aCode, -fValue, -fExpectedRoundVal );
     }
     {   // several sub-formats tdf#106052
         aCode = "0.00;-0.000";
         fValue = 1.0/3.0;
         fExpectedRoundVal = 0.33;
-        checkPrecisionAsShown( aCode, fValue, fExpectedRoundVal );
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
         fValue = -1.0/3.0;
         fExpectedRoundVal = -0.333;
-        checkPrecisionAsShown( aCode, fValue, fExpectedRoundVal );
+        checkPrecisionAsShown( aCode,  fValue,  fExpectedRoundVal );
     }
 
     setCalcAsShown( m_pDoc, false);
diff --git a/sc/source/core/data/documen4.cxx b/sc/source/core/data/documen4.cxx
index 6a1110b..6bb280b 100644
--- a/sc/source/core/data/documen4.cxx
+++ b/sc/source/core/data/documen4.cxx
@@ -660,7 +660,8 @@ double ScDocument::RoundValueAsShown( double fVal, sal_uInt32 nFormat ) const
         short nPrecision;
         if ((nFormat % SV_COUNTRY_LANGUAGE_OFFSET) != 0)
         {
-            nPrecision = (short)GetFormatTable()->GetFormatPrecision( nFormat, fVal );
+            sal_uInt16 nIdx = pFormat->GetSubformatIndex( fVal );
+            nPrecision = (short)pFormat->GetFormatPrecision( nIdx );
             switch ( nType )
             {
                 case css::util::NumberFormat::PERCENT:      // 0.41% == 0.0041
@@ -668,10 +669,23 @@ double ScDocument::RoundValueAsShown( double fVal, sal_uInt32 nFormat ) const
                     break;
                 case css::util::NumberFormat::SCIENTIFIC:   // 1.23e-3 == 0.00123
                 {
+                    short nExp = 0;
                     if ( fVal > 0.0 )
-                        nPrecision = sal::static_int_cast<short>( nPrecision - (short)floor( log10( fVal ) ) );
+                        nExp = (short)floor( log10( fVal ) );
                     else if ( fVal < 0.0 )
-                        nPrecision = sal::static_int_cast<short>( nPrecision - (short)floor( log10( -fVal ) ) );
+                        nExp = (short)floor( log10( -fVal ) );
+                    nPrecision -= nExp;
+                    short nInteger = (short)pFormat->GetFormatIntegerDigits( nIdx );
+                    if ( nInteger > 1 ) // Engineering notation
+                    {
+                        short nIncrement = nExp % nInteger;
+                        if ( nIncrement != 0 )
+                        {
+                            nPrecision += nIncrement;
+                            if (nExp < 0 )
+                                nPrecision += nInteger;
+                        }
+                    }
                     break;
                 }
                 case css::util::NumberFormat::FRACTION:     // get value of fraction representation


More information about the Libreoffice-commits mailing list