将数字转换为另一个数字

时间:2013-01-10 12:33:35

标签: php numberformatter

所以我有这个小问题。 我有一个表格,你可以输入一个数字,它可以是欧洲格式,逗号为十进制(,)或美国小数点(。)。

了解这一点,我如何转换欧洲美国格式的数字格式?美式格式保持不变?

我尝试使用这个公式,但结果是错误的。

$american = '12.5';
$european = '12,5';

$locale = 'it_IT.utf8';
setlocale ('LC_NUMERIC', $locale);
$fmt = new NumberFormatter ($locale, NumberFormatter::DECIMAL);
$american = $ fmt->parse($american);
$european = $ fmt->parse($europen);

response

$american = 125
$european = 12.5

问题出在哪里?

php ver。 5.4.10

2 个答案:

答案 0 :(得分:2)

尝试以下代码:

$american = '12.5';
$european = '12,5';

$locale = 'en_US.utf8';
$fmt = new NumberFormatter ($locale, NumberFormatter::DECIMAL);
$american = $fmt->parse($american);

$locale = 'it_IT.utf8';
$fmt = new NumberFormatter ($locale, NumberFormatter::DECIMAL);
$european = $fmt->parse($european);

获得所需的结果?

答案 1 :(得分:0)

不是更好地使用str_replace()吗?