将逗号作为小数点转换为float

时间:2022-12-28 17:07:56

I have a list of prices with a comma for a decimal point and a dot as the thousand separator.

我有一个价格列表,其中包含一个小数点的逗号和一个点作为千位分隔符。

Some examples:

一些例子:

12,30
116,10
1.563,14

12,30 116,10 1.563,14

These come in this format from a third party. I want to convert them to floats and add them together.

这些来自第三方。我想将它们转换为浮点数并将它们加在一起。

What is the best way to do this? number_format doesn't seem to work with this format, and str_replace seems like overkill, as I have to do it more that once on each number.

做这个的最好方式是什么? number_format似乎不适用于这种格式,str_replace似乎有点矫枉过正,因为我必须在每个数字上多做一次。

Is there are better way? Thanks.

有更好的方法吗?谢谢。

7 个解决方案

#1


80  

Using str_replace() to remove the dots is not overkill.

使用str_replace()删除点不是矫枉过正。

$string_number = '1.512.523,55';
// NOTE: You don't really have to use floatval() here, it's just to prove that it's a legitimate float value.
$number = floatval(str_replace(',', '.', str_replace('.', '', $string_number)));

// At this point, $number is a "natural" float.
print $number;

This is almost certainly the least CPU-intensive way you can do this, and odds are that even if you use some fancy function to do it, that this is what it does under the hood.

这几乎可以肯定是你可以做到这一点的CPU密集程度最低的方式,而且即使你使用一些奇特的功能来做这件事,这也就是它在幕后所做的事情。

#2


14  

If you're using PHP5.3 or above, you can use numfmt_parse to do "a reversed number_format". If you're not, you stuck with replacing the occurrances with preg_replace/str_replace.

如果您使用的是PHP5.3或更高版本,则可以使用numfmt_parse执行“反向number_format”。如果你不是,你坚持用preg_replace / str_replace替换出现的问题。

#3


10  

You could use the NumberFormatter class with its parse method.

您可以将NumberFormatter类与其解析方法一起使用。

#4


4  

Assuming they are in a file or array just do the replace as a batch (i.e. on all at once):

假设它们位于文件或数组中,只需将批量替换为批处理(即一次性完成):

$input = str_replace(array('.', ','), array('', '.'), $input); 

and then process the numbers from there taking full advantage of PHP's loosely typed nature.

然后从那里处理数字,充分利用PHP的松散类型性质。

#5


3  

This function is compatible for numbers with dots or commas as decimals

此函数兼容带点或逗号作为小数的数字

function floatvalue($val){
            $val = str_replace(",",".",$val);
            $val = preg_replace('/\.(?=.*\.)/', '', $val);
            return floatval($val);
}
$number = "1.325.125,54";
echo floatvalue($number); // The output is 1325125.54
$number = "1,325,125.54"; 
echo floatvalue($number); // The output is 1325125.54

#6


1  

from PHP manual:

来自PHP手册:

str_replace — Replace all occurrences of the search string with the replacement string

str_replace - 用替换字符串替换所有出现的搜索字符串

I would go down that route, and then convert from string to float - floatval

我会沿着那条路走下去,然后从string转换为float - floatval

#7


1  

Might look excessive but will convert any given format no mater the locale:

可能看起来过多,但会转换任何给定的格式,而不是区域设置:

function normalizeDecimal($val, int $precision = 4): string
{
    $input = str_replace(' ', '', $val);
    $number = str_replace(',', '.', $input);
    if (strpos($number, '.')) {
        $groups = explode('.', str_replace(',', '.', $number));
        $lastGroup = array_pop($groups);
        $number = implode('', $groups) . '.' . $lastGroup;
    }
    return bcadd($number, 0, $precision);
}

Output:

输出:

.12           -> 0.1200
123           -> 123.0000
123.91        -> 12345678.9100
123 456 78.91 -> 12345678.9100
123,456,78.91 -> 12345678.9100
123.456.78,91 -> 12345678.9100
123 456 78,91 -> 12345678.9100

#1


80  

Using str_replace() to remove the dots is not overkill.

使用str_replace()删除点不是矫枉过正。

$string_number = '1.512.523,55';
// NOTE: You don't really have to use floatval() here, it's just to prove that it's a legitimate float value.
$number = floatval(str_replace(',', '.', str_replace('.', '', $string_number)));

// At this point, $number is a "natural" float.
print $number;

This is almost certainly the least CPU-intensive way you can do this, and odds are that even if you use some fancy function to do it, that this is what it does under the hood.

这几乎可以肯定是你可以做到这一点的CPU密集程度最低的方式,而且即使你使用一些奇特的功能来做这件事,这也就是它在幕后所做的事情。

#2


14  

If you're using PHP5.3 or above, you can use numfmt_parse to do "a reversed number_format". If you're not, you stuck with replacing the occurrances with preg_replace/str_replace.

如果您使用的是PHP5.3或更高版本,则可以使用numfmt_parse执行“反向number_format”。如果你不是,你坚持用preg_replace / str_replace替换出现的问题。

#3


10  

You could use the NumberFormatter class with its parse method.

您可以将NumberFormatter类与其解析方法一起使用。

#4


4  

Assuming they are in a file or array just do the replace as a batch (i.e. on all at once):

假设它们位于文件或数组中,只需将批量替换为批处理(即一次性完成):

$input = str_replace(array('.', ','), array('', '.'), $input); 

and then process the numbers from there taking full advantage of PHP's loosely typed nature.

然后从那里处理数字,充分利用PHP的松散类型性质。

#5


3  

This function is compatible for numbers with dots or commas as decimals

此函数兼容带点或逗号作为小数的数字

function floatvalue($val){
            $val = str_replace(",",".",$val);
            $val = preg_replace('/\.(?=.*\.)/', '', $val);
            return floatval($val);
}
$number = "1.325.125,54";
echo floatvalue($number); // The output is 1325125.54
$number = "1,325,125.54"; 
echo floatvalue($number); // The output is 1325125.54

#6


1  

from PHP manual:

来自PHP手册:

str_replace — Replace all occurrences of the search string with the replacement string

str_replace - 用替换字符串替换所有出现的搜索字符串

I would go down that route, and then convert from string to float - floatval

我会沿着那条路走下去,然后从string转换为float - floatval

#7


1  

Might look excessive but will convert any given format no mater the locale:

可能看起来过多,但会转换任何给定的格式,而不是区域设置:

function normalizeDecimal($val, int $precision = 4): string
{
    $input = str_replace(' ', '', $val);
    $number = str_replace(',', '.', $input);
    if (strpos($number, '.')) {
        $groups = explode('.', str_replace(',', '.', $number));
        $lastGroup = array_pop($groups);
        $number = implode('', $groups) . '.' . $lastGroup;
    }
    return bcadd($number, 0, $precision);
}

Output:

输出:

.12           -> 0.1200
123           -> 123.0000
123.91        -> 12345678.9100
123 456 78.91 -> 12345678.9100
123,456,78.91 -> 12345678.9100
123.456.78,91 -> 12345678.9100
123 456 78,91 -> 12345678.9100