如何将number_format的输出转换回PHP中的数字? [英] How do I convert output of number_format back to numbers in PHP?
本文介绍了如何将number_format的输出转换回PHP中的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
PHP无法识别1,200.00
(由number_format
生成),但只能识别1200.00
,
PHP can't recognize 1,200.00
(generated by number_format
) but only 1200.00
,
该问题的一般解决方案是什么?
What's the general solution for this problem?
推荐答案
您可以删除不是数字或小数点的任何字符,并使用 floatval
:
You could remove any character that is not a digit or a decimal point and parse that with floatval
:
$number = 1200.00;
$parsed = floatval(preg_replace('/[^\d.]/', '', number_format($number)));
var_dump($number === $parsed); // bool(true)
如果数字不是.
作为小数点:
And if the number has not .
as decimal point:
function parse_number($number, $dec_point=null) {
if (empty($dec_point)) {
$locale = localeconv();
$dec_point = $locale['decimal_point'];
}
return floatval(str_replace($dec_point, '.', preg_replace('/[^\d'.preg_quote($dec_point).']/', '', $number)));
}
这篇关于如何将number_format的输出转换回PHP中的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文