diff --git a/NEWS b/NEWS index 357f290aacf2e..d231676da0624 100644 --- a/NEWS +++ b/NEWS @@ -104,6 +104,8 @@ Standard: . Fixed bug GH-12592 (strcspn() odd behaviour with NUL bytes and empty mask). (nielsdos) . Removed the deprecated inet_ntoa call support. (David Carlier) + . Cast large floats that are within int range to int in number_format so + the precision is not lost. (Marc Bennewitz) XML: . Added XML_OPTION_PARSE_HUGE parser option. (nielsdos) diff --git a/ext/standard/math.c b/ext/standard/math.c index 0cd7ecfbb5762..377c573033f69 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -1330,6 +1330,16 @@ PHP_FUNCTION(number_format) break; case IS_DOUBLE: + // double values of >= 2^52 can not have fractional digits anymore + // Casting to long on 64bit will not loose precision on rounding + if (UNEXPECTED( + (Z_DVAL_P(num) >= 4503599627370496.0 || Z_DVAL_P(num) <= -4503599627370496.0) + && ZEND_DOUBLE_FITS_LONG(Z_DVAL_P(num)) + )) { + RETURN_STR(_php_math_number_format_long((zend_long)Z_DVAL_P(num), dec, dec_point, dec_point_len, thousand_sep, thousand_sep_len)); + break; + } + if (dec >= 0) { dec_int = ZEND_LONG_INT_OVFL(dec) ? INT_MAX : (int)dec; } else { diff --git a/ext/standard/tests/math/number_format_basiclong_64bit.phpt b/ext/standard/tests/math/number_format_basiclong_64bit.phpt index 2f63091abb8c6..fd709fc648f92 100644 --- a/ext/standard/tests/math/number_format_basiclong_64bit.phpt +++ b/ext/standard/tests/math/number_format_basiclong_64bit.phpt @@ -12,10 +12,12 @@ define("MAX_32Bit", 2147483647); define("MIN_64Bit", -9223372036854775807 - 1); define("MIN_32Bit", -2147483647 - 1); -$longVals = array( +$numbers = array( MAX_64Bit, MIN_64Bit, MAX_32Bit, MIN_32Bit, MAX_64Bit - MAX_32Bit, MIN_64Bit - MIN_32Bit, MAX_32Bit + 1, MIN_32Bit - 1, MAX_32Bit * 2, (MAX_32Bit * 2) + 1, (MAX_32Bit * 2) - 1, - MAX_64Bit -1, MAX_64Bit + 1, MIN_64Bit + 1, MIN_64Bit - 1 + MAX_64Bit - 1, MAX_64Bit + 1, MIN_64Bit + 1, MIN_64Bit - 1, + // floats rounded as int + MAX_64Bit - 1024.0, MIN_64Bit + 1024.0 ); $precisions = array( @@ -31,12 +33,12 @@ $precisions = array( PHP_INT_MIN, ); -foreach ($longVals as $longVal) { +foreach ($numbers as $number) { echo "--- testing: "; - var_dump($longVal); + var_dump($number); foreach ($precisions as $precision) { echo "... with precision " . $precision . ": "; - var_dump(number_format($longVal, $precision)); + var_dump(number_format($number, $precision)); } } @@ -199,8 +201,30 @@ foreach ($longVals as $longVal) { --- testing: float(-9.223372036854776E+18) ... with precision 5: string(32) "-9,223,372,036,854,775,808.00000" ... with precision 0: string(26) "-9,223,372,036,854,775,808" -... with precision -1: string(26) "-9,223,372,036,854,775,808" -... with precision -5: string(26) "-9,223,372,036,854,800,384" +... with precision -1: string(26) "-9,223,372,036,854,775,810" +... with precision -5: string(26) "-9,223,372,036,854,800,000" +... with precision -10: string(26) "-9,223,372,040,000,000,000" +... with precision -11: string(26) "-9,223,372,000,000,000,000" +... with precision -17: string(26) "-9,200,000,000,000,000,000" +... with precision -19: string(27) "-10,000,000,000,000,000,000" +... with precision -20: string(1) "0" +... with precision -9223372036854775808: string(1) "0" +--- testing: float(9.223372036854775E+18) +... with precision 5: string(31) "9,223,372,036,854,774,784.00000" +... with precision 0: string(25) "9,223,372,036,854,774,784" +... with precision -1: string(25) "9,223,372,036,854,774,780" +... with precision -5: string(25) "9,223,372,036,854,800,000" +... with precision -10: string(25) "9,223,372,040,000,000,000" +... with precision -11: string(25) "9,223,372,000,000,000,000" +... with precision -17: string(25) "9,200,000,000,000,000,000" +... with precision -19: string(26) "10,000,000,000,000,000,000" +... with precision -20: string(1) "0" +... with precision -9223372036854775808: string(1) "0" +--- testing: float(-9.223372036854775E+18) +... with precision 5: string(32) "-9,223,372,036,854,774,784.00000" +... with precision 0: string(26) "-9,223,372,036,854,774,784" +... with precision -1: string(26) "-9,223,372,036,854,774,780" +... with precision -5: string(26) "-9,223,372,036,854,800,000" ... with precision -10: string(26) "-9,223,372,040,000,000,000" ... with precision -11: string(26) "-9,223,372,000,000,000,000" ... with precision -17: string(26) "-9,200,000,000,000,000,000"