Skip to content

Commit 9cb5221

Browse files
committed
Allow number_format to be only passed 3 arguments
Closes GH-5977
1 parent 7be61be commit 9cb5221

File tree

3 files changed

+35
-24
lines changed

3 files changed

+35
-24
lines changed

ext/standard/math.c

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,6 @@ PHP_FUNCTION(number_format)
10971097
double num;
10981098
zend_long dec = 0;
10991099
char *thousand_sep = NULL, *dec_point = NULL;
1100-
char thousand_sep_chr = ',', dec_point_chr = '.';
11011100
size_t thousand_sep_len = 0, dec_point_len = 0;
11021101

11031102
ZEND_PARSE_PARAMETERS_START(1, 4)
@@ -1108,30 +1107,16 @@ PHP_FUNCTION(number_format)
11081107
Z_PARAM_STRING_OR_NULL(thousand_sep, thousand_sep_len)
11091108
ZEND_PARSE_PARAMETERS_END();
11101109

1111-
switch(ZEND_NUM_ARGS()) {
1112-
case 1:
1113-
RETURN_STR(_php_math_number_format(num, 0, dec_point_chr, thousand_sep_chr));
1114-
break;
1115-
case 2:
1116-
RETURN_STR(_php_math_number_format(num, (int)dec, dec_point_chr, thousand_sep_chr));
1117-
break;
1118-
case 4:
1119-
if (dec_point == NULL) {
1120-
dec_point = &dec_point_chr;
1121-
dec_point_len = 1;
1122-
}
1123-
1124-
if (thousand_sep == NULL) {
1125-
thousand_sep = &thousand_sep_chr;
1126-
thousand_sep_len = 1;
1127-
}
1128-
1129-
RETVAL_STR(_php_math_number_format_ex(num, (int)dec,
1130-
dec_point, dec_point_len, thousand_sep, thousand_sep_len));
1131-
break;
1132-
default:
1133-
WRONG_PARAM_COUNT;
1110+
if (dec_point == NULL) {
1111+
dec_point = ".";
1112+
dec_point_len = 1;
1113+
}
1114+
if (thousand_sep == NULL) {
1115+
thousand_sep = ",";
1116+
thousand_sep_len = 1;
11341117
}
1118+
1119+
RETURN_STR(_php_math_number_format_ex(num, (int)dec, dec_point, dec_point_len, thousand_sep, thousand_sep_len));
11351120
}
11361121
/* }}} */
11371122

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
number_format should use default thousands seperator when 3 arguments are used
3+
--FILE--
4+
<?php
5+
6+
$number = 2020.1415;
7+
8+
var_dump(number_format($number, 2, 'F'));
9+
10+
?>
11+
--EXPECT--
12+
string(8) "2,020F14"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
number_format should use default values when passed null
3+
--FILE--
4+
<?php
5+
6+
$number = 2020.1415;
7+
8+
var_dump(number_format($number, 2, null, 'T'));
9+
var_dump(number_format($number, 2, 'F', null));
10+
11+
?>
12+
--EXPECT--
13+
string(8) "2T020.14"
14+
string(8) "2,020F14"

0 commit comments

Comments
 (0)