如何将 number_format 的输出转换回 PHP 中的数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2935906/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I convert output of number_format back to numbers in PHP?
提问by user198729
PHP can't recognize 1,200.00(generated by number_format) but only 1200.00,
PHP 无法识别1,200.00(由 生成number_format),但只能识别1200.00,
What's the general solution for this problem?
这个问题的一般解决方案是什么?
采纳答案by Gumbo
You could remove any character that is not a digit or a decimal point and parse that with floatval:
您可以删除任何不是数字或小数点的字符,然后使用以下命令对其进行解析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)));
}
回答by Donnie
If you're using 5.3 or higher (thanks ircmaxell), use numfmt_parse.
如果您使用 5.3 或更高版本(感谢 ircmaxell),请使用numfmt_parse。
回答by Frederic
You can use filter_var. e.g.
您可以使用 filter_var。例如
$num = '1,200,998';
$real_integer = filter_var($num, FILTER_SANITIZE_NUMBER_INT);
echo $real_integer;
will output:
将输出:
1200998
回答by Manuel MT
use ; Sanitize filters
用 ; 消毒过滤器
$number1= '$ 1,989.34';
$number1='$1,989.34';
    $number2 = filter_var($number, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);//1989.34
    $number2 = filter_var($number, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_THOUSAND);//1,98934
回答by Shyam
The better way is to use the options in number_formatfunction itself. The format is
更好的方法是使用number_format函数本身的选项。格式是
number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
I.e. if you don't need the ','in the formatted string make, it is like
即,如果您不需要','格式化字符串中的 make,就像
$num=23.33333
echo number_format($num,2,'.','');
If you need that as float then typecast the same to float
如果你需要它作为浮动然后将相同的类型转换为浮动
$floatnum= (float) number_format($num,2,'.','');
回答by ircmaxell
You could do $num = (float) str_replace(',', '', $num);Basically, just get rid of the commas, and then cast it as a numeric type (float or int).
你可以做$num = (float) str_replace(',', '', $num);基本上,只需去掉逗号,然后将其转换为数字类型(浮点数或整数)。
回答by birdsaremagic
I use the following code:
我使用以下代码:
function remove_non_numerics($str)
{ 
    $temp       = trim($str);
    $result  = "";
    $pattern    = '/[^0-9]*/';
    $result     = preg_replace($pattern, '', $temp);
    return $result;
}
回答by Alireza Rahmani Khalili
In this case you can, use PHP roundmethod like: round (12.000000, 2);
在这种情况下,您可以使用 PHPround方法,例如:round (12.000000, 2);
回答by user2598395
use "[^0-9.]+" regex.
使用“[^0-9.]+”正则表达式。
$formattedVal = ',99,999.99';
$unformattedVal = (float)preg_replace("/[^0-9.]+/", "", $formattedVal);
var_dump($unformattedVal);
worked for me.
为我工作。

