summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/svl/zformat.hxx3
-rw-r--r--sc/qa/unit/ucalc.cxx105
-rw-r--r--sc/source/core/data/documen4.cxx20
3 files changed, 98 insertions, 30 deletions
diff --git a/include/svl/zformat.hxx b/include/svl/zformat.hxx
index 34525acad43e..4d108815d9a7 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, 5000, ... and only by the number formatter!
diff --git a/sc/qa/unit/ucalc.cxx b/sc/qa/unit/ucalc.cxx
index dbdd549b3ba6..ea44ff7d4f3d 100644
--- a/sc/qa/unit/ucalc.cxx
+++ b/sc/qa/unit/ucalc.cxx
@@ -6434,12 +6434,14 @@ 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 buguous!!!! tdf#106253
+ { // thousand rounding bogus!!!! tdf#106253
aCode = "0,,";
fValue = 4.0e9 / 7.0;
fExpectedRoundVal = 571e6; // actual is 571428571
@@ -6452,54 +6454,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 bugous!!! 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