From fdf982f70b2944053d995baaa3d78c7cdc4bbc4b Mon Sep 17 00:00:00 2001 From: Eike Rathke Date: Thu, 14 Jan 2016 20:25:01 +0100 Subject: handle negative decimal places for rounding, tdf#96918 related Change-Id: Ifa423eabc64ead519c4f4a3370a06e88ea5c7466 --- sal/rtl/math.cxx | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'sal/rtl') 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(nInt) == fValue) { - if (nDecPlaces == rtl_math_DecimalPlaces_Max || bEraseTrailingDecZeros) + if (nDecPlaces == rtl_math_DecimalPlaces_Max) nDecPlaces = 0; else - nDecPlaces = ::std::min(nDecPlaces, 15); + nDecPlaces = ::std::max( ::std::min( nDecPlaces, 15), -15); + if (bEraseTrailingDecZeros && nDecPlaces > 0) + nDecPlaces = 0; + + // Round before decimal position. + if (nDecPlaces < 0) + { + sal_Int64 nRounding = static_cast( pow( 10.0, static_cast( -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--) -- cgit