Using setlocale() doesn't affect PHPs number conversions
I have the following script:
<?php
$test = "2.5";
echo (float)$test;
echo "\n";
$r = setlocale(LC_ALL, "da_DK.UTF8");
setlocale(LC_ALL, NULL);
print_r(localeconv());
echo "\n";
echo (float)$test;
echo "\n";
echo (float)"2,5";
echo "\n";
?>
Which generates the following output:
2.5
Array
(
[decimal_point] => ,
[thousands_sep] => .
[int_curr_symbol] => DKK
[currency_symbol] => kr
[mon_decimal_point] => ,
[mon_thousands_sep] => .
[positive_sign] =>
[negative_sign] => -
[int_frac_digits] => 2
[frac_digits] => 2
[p_cs_precedes] => 1
[p_sep_by_s开发者_Go百科pace] => 2
[n_cs_precedes] => 1
[n_sep_by_space] => 2
[p_sign_posn] => 4
[n_sign_posn] => 4
[grouping] => Array
(
[0] => 3
[1] => 3
)
[mon_grouping] => Array
(
[0] => 3
[1] => 3
)
)
2,5
2
The very last line which reads 2
- I would have expected that to read 2,5
- and as far as I can see on the PHP documentation, it should.
setlocale
is omitted then the output of localeconv()
is inconsistent with the danish locale - for reasons that are unclear to me. (float)"2,5"
equals 2 (note the comma) whereas (float)"2.5"
equals 2.5. The reason can be read in the manual:
If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float.
Type casting is not affected by setlocale()
.
精彩评论