diff options
author | Laurent Balland-Poirier <laurent.balland-poirier@laposte.net> | 2017-03-03 21:51:26 +0100 |
---|---|---|
committer | Eike Rathke <erack@redhat.com> | 2017-03-14 16:31:45 +0000 |
commit | 5f2db66ad0de6fbbae309850516e17eaa17934cb (patch) | |
tree | 5a699ffb72903c13068391a4fa39a92a12dc374d | |
parent | 578c8dea8ec7d377d738f8d821b741d0f3e8b9e4 (diff) |
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@libreoffice.org>
Reviewed-by: Eike Rathke <erack@redhat.com>
-rw-r--r-- | include/svl/zformat.hxx | 3 | ||||
-rw-r--r-- | sc/qa/unit/ucalc.cxx | 103 | ||||
-rw-r--r-- | sc/source/core/data/documen4.cxx | 20 |
3 files changed, 97 insertions, 29 deletions
diff --git a/include/svl/zformat.hxx b/include/svl/zformat.hxx index 85250ac23e08..1dd05d2e9146 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 9a51d92f7a96..86aa87798477 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 6a1110b6cd80..6bb280b20d91 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 |