diff options
author | Eike Rathke <erack@redhat.com> | 2016-01-14 20:25:01 +0100 |
---|---|---|
committer | Eike Rathke <erack@redhat.com> | 2016-01-14 20:53:29 +0100 |
commit | fdf982f70b2944053d995baaa3d78c7cdc4bbc4b (patch) | |
tree | e8ebe98d4123d4818f77deda622bd6e0ecc9c19d /sal/rtl | |
parent | a669faa1fa88d4d82d3f1d38b652b689ecc4d3a1 (diff) |
handle negative decimal places for rounding, tdf#96918 related
Change-Id: Ifa423eabc64ead519c4f4a3370a06e88ea5c7466
Diffstat (limited to 'sal/rtl')
-rw-r--r-- | sal/rtl/math.cxx | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx index f3822671522d..e621c4807a92 100644 --- a/sal/rtl/math.cxx +++ b/sal/rtl/math.cxx @@ -218,10 +218,27 @@ inline void doubleToString(StringT ** pResult, // true within the precision range. if (nInt <= kMaxInt && static_cast<double>(nInt) == fValue) { - if (nDecPlaces == rtl_math_DecimalPlaces_Max || bEraseTrailingDecZeros) + if (nDecPlaces == rtl_math_DecimalPlaces_Max) nDecPlaces = 0; else - nDecPlaces = ::std::min<sal_Int32>(nDecPlaces, 15); + nDecPlaces = ::std::max<sal_Int32>( ::std::min<sal_Int32>( nDecPlaces, 15), -15); + if (bEraseTrailingDecZeros && nDecPlaces > 0) + nDecPlaces = 0; + + // Round before decimal position. + if (nDecPlaces < 0) + { + sal_Int64 nRounding = static_cast<sal_Int64>( pow( 10.0, static_cast<double>( -nDecPlaces - 1))); + sal_Int64 nTemp = nInt / nRounding; + int nDigit = nTemp % 10; + nTemp /= 10; + if (nDigit >= 5) + ++nTemp; + nTemp *= 10; + nTemp *= nRounding; + nInt = nTemp; + nDecPlaces = 0; + } // Max 1 sign, 16 integer digits, 15 group separators, 1 decimal // separator, 15 decimals digits. @@ -258,7 +275,7 @@ inline void doubleToString(StringT ** pResult, pBuf[i] = c; } // Append decimals. - if (nDecPlaces) + if (nDecPlaces > 0) { *p++ = cDecSeparator; while (nDecPlaces--) |