Skip to content

Commit ec90ecc

Browse files
committed
Allow number_format to be only passed 3 arguments
1 parent 45d69fb commit ec90ecc

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

ext/standard/math.c

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,9 +1096,8 @@ PHP_FUNCTION(number_format)
10961096
{
10971097
double num;
10981098
zend_long dec = 0;
1099-
char *thousand_sep = NULL, *dec_point = NULL;
1100-
char thousand_sep_chr = ',', dec_point_chr = '.';
1101-
size_t thousand_sep_len = 0, dec_point_len = 0;
1099+
char *thousand_sep = ",", *dec_point = NULL;
1100+
size_t thousand_sep_len = 1, dec_point_len = 0;
11021101

11031102
ZEND_PARSE_PARAMETERS_START(1, 4)
11041103
Z_PARAM_DOUBLE(num)
@@ -1108,30 +1107,13 @@ 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+
char dec_point_chr = '.';
1112+
dec_point = &dec_point_chr;
1113+
dec_point_len = 1;
11341114
}
1115+
1116+
RETURN_STR(_php_math_number_format_ex(num, (int)dec, dec_point, dec_point_len, thousand_sep, thousand_sep_len));
11351117
}
11361118
/* }}} */
11371119

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"

0 commit comments

Comments
 (0)