mirror of
https://github.com/php/php-src.git
synced 2026-03-24 00:02:20 +01:00
number_format() Support rounding negative places
Closes GH-11487
This commit is contained in:
committed by
Jakub Zelenka
parent
7c5a57077a
commit
e85fb0902c
4
NEWS
4
NEWS
@@ -23,6 +23,10 @@ PHP NEWS
|
||||
. Fixed GH-11573 (RecursiveDirectoryIterator::hasChildren is slow).
|
||||
(nielsdos)
|
||||
|
||||
- Standard:
|
||||
. Added support for rounding negative places in number_format().
|
||||
(Marc Bennewitz)
|
||||
|
||||
- Streams:
|
||||
. Implemented GH-11242 (_php_stream_copy_to_mem: Allow specifying a maximum
|
||||
length without allocating a buffer of that size). (Jakub Zelenka)
|
||||
|
||||
@@ -219,6 +219,10 @@ PHP 8.3 UPGRADE NOTES
|
||||
Previously arrays and objects where ignored whilst every other value was cast to int.
|
||||
Moreover, objects that define a numeric cast (e.g. GMP) are now casted instead of ignored.
|
||||
RFC: https://wiki.php.net/rfc/saner-array-sum-product
|
||||
. number_format() $decimal parameter handles rounding to negative places. It
|
||||
means that when $decimals is negative, $num is rounded to $decimals
|
||||
significant digits before the decimal point. Previously negative $decimals
|
||||
got silently ignored and the number got rounded to zero decimal places.
|
||||
|
||||
========================================
|
||||
6. New Functions
|
||||
|
||||
@@ -1030,8 +1030,8 @@ PHPAPI zend_string *_php_math_number_format_ex(double d, int dec, const char *de
|
||||
d = -d;
|
||||
}
|
||||
|
||||
dec = MAX(0, dec);
|
||||
d = _php_math_round(d, dec, PHP_ROUND_HALF_UP);
|
||||
dec = MAX(0, dec);
|
||||
tmpbuf = strpprintf(0, "%.*F", dec, d);
|
||||
if (tmpbuf == NULL) {
|
||||
return NULL;
|
||||
|
||||
151
ext/standard/tests/math/number_format_decimals.phpt
Normal file
151
ext/standard/tests/math/number_format_decimals.phpt
Normal file
@@ -0,0 +1,151 @@
|
||||
--TEST--
|
||||
Test number_format() - test function with different decimal places
|
||||
--FILE--
|
||||
<?php
|
||||
$values = array(
|
||||
1.5151,
|
||||
15.151,
|
||||
151.51,
|
||||
1515.1,
|
||||
15151,
|
||||
-1.5151,
|
||||
-15.151,
|
||||
-151.51,
|
||||
-1515.1,
|
||||
-15151
|
||||
);
|
||||
|
||||
$decimals = array(0, 1, 2, 3, 4, 5, -1, -2, -3, -4, -5);
|
||||
|
||||
foreach ($values as $value) {
|
||||
echo 'testing ';
|
||||
var_dump($value);
|
||||
|
||||
foreach ($decimals as $decimal) {
|
||||
echo '... with decimal places of ' . $decimal . ': ';
|
||||
var_dump(number_format($value, $decimal));
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECT--
|
||||
testing float(1.5151)
|
||||
... with decimal places of 0: string(1) "2"
|
||||
... with decimal places of 1: string(3) "1.5"
|
||||
... with decimal places of 2: string(4) "1.52"
|
||||
... with decimal places of 3: string(5) "1.515"
|
||||
... with decimal places of 4: string(6) "1.5151"
|
||||
... with decimal places of 5: string(7) "1.51510"
|
||||
... with decimal places of -1: string(1) "0"
|
||||
... with decimal places of -2: string(1) "0"
|
||||
... with decimal places of -3: string(1) "0"
|
||||
... with decimal places of -4: string(1) "0"
|
||||
... with decimal places of -5: string(1) "0"
|
||||
testing float(15.151)
|
||||
... with decimal places of 0: string(2) "15"
|
||||
... with decimal places of 1: string(4) "15.2"
|
||||
... with decimal places of 2: string(5) "15.15"
|
||||
... with decimal places of 3: string(6) "15.151"
|
||||
... with decimal places of 4: string(7) "15.1510"
|
||||
... with decimal places of 5: string(8) "15.15100"
|
||||
... with decimal places of -1: string(2) "20"
|
||||
... with decimal places of -2: string(1) "0"
|
||||
... with decimal places of -3: string(1) "0"
|
||||
... with decimal places of -4: string(1) "0"
|
||||
... with decimal places of -5: string(1) "0"
|
||||
testing float(151.51)
|
||||
... with decimal places of 0: string(3) "152"
|
||||
... with decimal places of 1: string(5) "151.5"
|
||||
... with decimal places of 2: string(6) "151.51"
|
||||
... with decimal places of 3: string(7) "151.510"
|
||||
... with decimal places of 4: string(8) "151.5100"
|
||||
... with decimal places of 5: string(9) "151.51000"
|
||||
... with decimal places of -1: string(3) "150"
|
||||
... with decimal places of -2: string(3) "200"
|
||||
... with decimal places of -3: string(1) "0"
|
||||
... with decimal places of -4: string(1) "0"
|
||||
... with decimal places of -5: string(1) "0"
|
||||
testing float(1515.1)
|
||||
... with decimal places of 0: string(5) "1,515"
|
||||
... with decimal places of 1: string(7) "1,515.1"
|
||||
... with decimal places of 2: string(8) "1,515.10"
|
||||
... with decimal places of 3: string(9) "1,515.100"
|
||||
... with decimal places of 4: string(10) "1,515.1000"
|
||||
... with decimal places of 5: string(11) "1,515.10000"
|
||||
... with decimal places of -1: string(5) "1,520"
|
||||
... with decimal places of -2: string(5) "1,500"
|
||||
... with decimal places of -3: string(5) "2,000"
|
||||
... with decimal places of -4: string(1) "0"
|
||||
... with decimal places of -5: string(1) "0"
|
||||
testing int(15151)
|
||||
... with decimal places of 0: string(6) "15,151"
|
||||
... with decimal places of 1: string(8) "15,151.0"
|
||||
... with decimal places of 2: string(9) "15,151.00"
|
||||
... with decimal places of 3: string(10) "15,151.000"
|
||||
... with decimal places of 4: string(11) "15,151.0000"
|
||||
... with decimal places of 5: string(12) "15,151.00000"
|
||||
... with decimal places of -1: string(6) "15,150"
|
||||
... with decimal places of -2: string(6) "15,200"
|
||||
... with decimal places of -3: string(6) "15,000"
|
||||
... with decimal places of -4: string(6) "20,000"
|
||||
... with decimal places of -5: string(1) "0"
|
||||
testing float(-1.5151)
|
||||
... with decimal places of 0: string(2) "-2"
|
||||
... with decimal places of 1: string(4) "-1.5"
|
||||
... with decimal places of 2: string(5) "-1.52"
|
||||
... with decimal places of 3: string(6) "-1.515"
|
||||
... with decimal places of 4: string(7) "-1.5151"
|
||||
... with decimal places of 5: string(8) "-1.51510"
|
||||
... with decimal places of -1: string(1) "0"
|
||||
... with decimal places of -2: string(1) "0"
|
||||
... with decimal places of -3: string(1) "0"
|
||||
... with decimal places of -4: string(1) "0"
|
||||
... with decimal places of -5: string(1) "0"
|
||||
testing float(-15.151)
|
||||
... with decimal places of 0: string(3) "-15"
|
||||
... with decimal places of 1: string(5) "-15.2"
|
||||
... with decimal places of 2: string(6) "-15.15"
|
||||
... with decimal places of 3: string(7) "-15.151"
|
||||
... with decimal places of 4: string(8) "-15.1510"
|
||||
... with decimal places of 5: string(9) "-15.15100"
|
||||
... with decimal places of -1: string(3) "-20"
|
||||
... with decimal places of -2: string(1) "0"
|
||||
... with decimal places of -3: string(1) "0"
|
||||
... with decimal places of -4: string(1) "0"
|
||||
... with decimal places of -5: string(1) "0"
|
||||
testing float(-151.51)
|
||||
... with decimal places of 0: string(4) "-152"
|
||||
... with decimal places of 1: string(6) "-151.5"
|
||||
... with decimal places of 2: string(7) "-151.51"
|
||||
... with decimal places of 3: string(8) "-151.510"
|
||||
... with decimal places of 4: string(9) "-151.5100"
|
||||
... with decimal places of 5: string(10) "-151.51000"
|
||||
... with decimal places of -1: string(4) "-150"
|
||||
... with decimal places of -2: string(4) "-200"
|
||||
... with decimal places of -3: string(1) "0"
|
||||
... with decimal places of -4: string(1) "0"
|
||||
... with decimal places of -5: string(1) "0"
|
||||
testing float(-1515.1)
|
||||
... with decimal places of 0: string(6) "-1,515"
|
||||
... with decimal places of 1: string(8) "-1,515.1"
|
||||
... with decimal places of 2: string(9) "-1,515.10"
|
||||
... with decimal places of 3: string(10) "-1,515.100"
|
||||
... with decimal places of 4: string(11) "-1,515.1000"
|
||||
... with decimal places of 5: string(12) "-1,515.10000"
|
||||
... with decimal places of -1: string(6) "-1,520"
|
||||
... with decimal places of -2: string(6) "-1,500"
|
||||
... with decimal places of -3: string(6) "-2,000"
|
||||
... with decimal places of -4: string(1) "0"
|
||||
... with decimal places of -5: string(1) "0"
|
||||
testing int(-15151)
|
||||
... with decimal places of 0: string(7) "-15,151"
|
||||
... with decimal places of 1: string(9) "-15,151.0"
|
||||
... with decimal places of 2: string(10) "-15,151.00"
|
||||
... with decimal places of 3: string(11) "-15,151.000"
|
||||
... with decimal places of 4: string(12) "-15,151.0000"
|
||||
... with decimal places of 5: string(13) "-15,151.00000"
|
||||
... with decimal places of -1: string(7) "-15,150"
|
||||
... with decimal places of -2: string(7) "-15,200"
|
||||
... with decimal places of -3: string(7) "-15,000"
|
||||
... with decimal places of -4: string(7) "-20,000"
|
||||
... with decimal places of -5: string(1) "0"
|
||||
Reference in New Issue
Block a user