问题
When I try to cast
$value = floatval('14,5833');
to a float
type I expect a value with dot like 14.5833
but it returns me 14,5833
.
How should I do this ?
I wouldn't like to use any string replace functions.
回答1:
You have two options:
- Set locale to something that uses a dot instead of a coma. E.g. setLocale(LC_ALL, 'fr_BE.UTF-8');
- Keep using comma internally, and when you want to output that number, use number_format
回答2:
An example was helpful for me:
<?php
setlocale(LC_NUMERIC, 'en_US');
echo 1.234; // 1.234
setlocale(LC_NUMERIC, 'et_EE.UTF-8');
echo 1.234; // 1,234
echo number_format( 1.234, 2, '.', '' ); // 1.23
?>
回答3:
Check decimal_point
from localeconv and setlocale
来源:https://stackoverflow.com/questions/2996743/cast-string-with-comma-to-float-with-dot