summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Peter Banyard <girgias@php.net>2020-08-12 19:26:54 +0200
committerGeorge Peter Banyard <girgias@php.net>2020-08-13 18:07:17 +0200
commit9cb522166c64ddb5e161857681e17e820db70255 (patch)
treef71c6e440026f15feabf8c50a4513ece26eb0819
parent7be61bec8037e601c989e5002198f77bf39e8d59 (diff)
downloadphp-git-9cb522166c64ddb5e161857681e17e820db70255.tar.gz
Allow number_format to be only passed 3 arguments
Closes GH-5977
-rw-r--r--ext/standard/math.c33
-rw-r--r--ext/standard/tests/math/number_format_with_3_args.phpt12
-rw-r--r--ext/standard/tests/math/number_format_with_null_args.phpt14
3 files changed, 35 insertions, 24 deletions
diff --git a/ext/standard/math.c b/ext/standard/math.c
index d0c2708256..c5e0234fde 100644
--- a/ext/standard/math.c
+++ b/ext/standard/math.c
@@ -1097,7 +1097,6 @@ PHP_FUNCTION(number_format)
double num;
zend_long dec = 0;
char *thousand_sep = NULL, *dec_point = NULL;
- char thousand_sep_chr = ',', dec_point_chr = '.';
size_t thousand_sep_len = 0, dec_point_len = 0;
ZEND_PARSE_PARAMETERS_START(1, 4)
@@ -1108,30 +1107,16 @@ PHP_FUNCTION(number_format)
Z_PARAM_STRING_OR_NULL(thousand_sep, thousand_sep_len)
ZEND_PARSE_PARAMETERS_END();
- switch(ZEND_NUM_ARGS()) {
- case 1:
- RETURN_STR(_php_math_number_format(num, 0, dec_point_chr, thousand_sep_chr));
- break;
- case 2:
- RETURN_STR(_php_math_number_format(num, (int)dec, dec_point_chr, thousand_sep_chr));
- break;
- case 4:
- if (dec_point == NULL) {
- dec_point = &dec_point_chr;
- dec_point_len = 1;
- }
-
- if (thousand_sep == NULL) {
- thousand_sep = &thousand_sep_chr;
- thousand_sep_len = 1;
- }
-
- RETVAL_STR(_php_math_number_format_ex(num, (int)dec,
- dec_point, dec_point_len, thousand_sep, thousand_sep_len));
- break;
- default:
- WRONG_PARAM_COUNT;
+ if (dec_point == NULL) {
+ dec_point = ".";
+ dec_point_len = 1;
+ }
+ if (thousand_sep == NULL) {
+ thousand_sep = ",";
+ thousand_sep_len = 1;
}
+
+ RETURN_STR(_php_math_number_format_ex(num, (int)dec, dec_point, dec_point_len, thousand_sep, thousand_sep_len));
}
/* }}} */
diff --git a/ext/standard/tests/math/number_format_with_3_args.phpt b/ext/standard/tests/math/number_format_with_3_args.phpt
new file mode 100644
index 0000000000..72419ea0e8
--- /dev/null
+++ b/ext/standard/tests/math/number_format_with_3_args.phpt
@@ -0,0 +1,12 @@
+--TEST--
+number_format should use default thousands seperator when 3 arguments are used
+--FILE--
+<?php
+
+$number = 2020.1415;
+
+var_dump(number_format($number, 2, 'F'));
+
+?>
+--EXPECT--
+string(8) "2,020F14"
diff --git a/ext/standard/tests/math/number_format_with_null_args.phpt b/ext/standard/tests/math/number_format_with_null_args.phpt
new file mode 100644
index 0000000000..f99a06b980
--- /dev/null
+++ b/ext/standard/tests/math/number_format_with_null_args.phpt
@@ -0,0 +1,14 @@
+--TEST--
+number_format should use default values when passed null
+--FILE--
+<?php
+
+$number = 2020.1415;
+
+var_dump(number_format($number, 2, null, 'T'));
+var_dump(number_format($number, 2, 'F', null));
+
+?>
+--EXPECT--
+string(8) "2T020.14"
+string(8) "2,020F14"