How do I convert output of number_format back to numbers in PHP?
PHP can't rec开发者_如何学运维ognize 1,200.00
(generated by number_format
) but only 1200.00
,
What's the general solution for this problem?
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)));
}
use ; Sanitize filters
$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
If you're using 5.3 or higher (thanks ircmaxell), use numfmt_parse.
You can use filter_var. e.g.
$num = '1,200,998';
$real_integer = filter_var($num, FILTER_SANITIZE_NUMBER_INT);
echo $real_integer;
will output:
1200998
The better way is to use the options in number_format
function itself. The format is
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
$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,'.','');
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).
I use the following code:
function remove_non_numerics($str)
{
$temp = trim($str);
$result = "";
$pattern = '/[^0-9]*/';
$result = preg_replace($pattern, '', $temp);
return $result;
}
In this case you can, use PHP round
method like: round (12.000000, 2);
I couldn't find a functioning solution that would convert any kind of formatted number back to normal number in php. I needed it to work with any kind of number.
So it took a while, but I wrote it.
It works if the number has up to 2 decimals, but I think that's enough.
Also it will remove any currency symbol and will only allow digits, dots and commas to be passed.
function unformatNumber($number){
// works with 2 decimals max
$number = trim((string)$number);
$number = preg_replace('/&.*?;/', '', $number);
$number_b="";
$number_ar_prep=str_split($number);
$number_ar=array();
$separators_amount=0;
$numbers_after_separator=array();
$chars_after_last_separator=0;
$last_decimals=array();
$last_decimal="";
foreach ($number_ar_prep as $char) {
if((is_numeric($char) || $char=="," || $char==".")){
if(is_numeric($char)){
$number_ar[]=(float)$char;
}else{
$number_ar[]=$char;
}
}
}
foreach ($number_ar as $char) {
$char=trim($char);
$sep_increase=false;
if($char=="."){
$separators_amount++;
$sep_increase=true;
}elseif($char==","){
$separators_amount++;
$sep_increase=true;
}
$number_b.=$char;
if($separators_amount>0){
if($sep_increase){
$chars_after_last_separator=0;
$last_decimal="";
}else{
$chars_after_last_separator++;
$last_decimal.=$char;
}
$numbers_after_separator[$separators_amount]=$chars_after_last_separator;
$last_decimals[$separators_amount]=$last_decimal;
}
}
$has_decimals=0;
if(isset($numbers_after_separator[$separators_amount])){
if($numbers_after_separator[$separators_amount]<3){
$has_decimals=1;
}
}
if(!$has_decimals){
$clean_number = str_replace(",","",$number_b);
$clean_number = str_replace(".","",$clean_number);
}else{
$decimals = $last_decimals[$separators_amount];
$num_without_decimals = preg_replace('/'.$decimals.'$/', '', $number_b);
$clean_num_without_dec = str_replace(",","",$num_without_decimals);
$clean_num_without_dec = str_replace(".","",$clean_num_without_dec);
$clean_number = $clean_num_without_dec.'.'.$decimals;
}
return (float)$clean_number;
}
Example output:
(original > unformatted)
1 1
2 2
2.00 2
2,00 2
2,31 2.31
2.31 2.31
24 24
24.00 24
24,00 24
24,31 24.31
25.31 25.31
244 244
244.00 244
244,00 244
244,31 244.31
255.31 255.31
2000 2000
2000.31 2000.31
2000,31 2000.31
2.000 2000
2,000 2000
2.000,31 2000.31
2,000.31 2000.31
20000 20000
20.000 20000
20,000 20000
20.000,31 20000.31
20,000.31 20000.31
200000 200000
200.000 200000
200,000 200000
200.000,15 200000.15
204,000.31 204000.31
2000000 2000000
2.000.000 2000000
2.050,000 2050000
2.500.000,51 2500000.51
2.000,000.31 2000000.31
24,30 € 24.3
Thanks to @ircmaxell, I came up with this:
$t_sep = localeconv()['thousands_sep'];
$num = (float) str_replace($t_sep? $t_sep : ',', '', $numStr);
use "[^0-9.]+" regex.
$formattedVal = '$9,99,999.99';
$unformattedVal = (float)preg_replace("/[^0-9.]+/", "", $formattedVal);
var_dump($unformattedVal);
worked for me.
精彩评论