将货币格式的字符串转换为浮点数

发布于 2024-11-16 15:45:34 字数 2529 浏览 1 评论 0原文

我必须读取一个相当大的文件,其中包含以不同方式格式化的数字。

我尝试使用内置的 floatval() 函数。此函数适用于某些数字,例如 22000.76,但不适用于 22000,76

阅读 php.net 上的评论对我帮助很大,我发现了这个 parseFloat 函数:

<?php 
function parseFloat($ptString) { 
        if (strlen($ptString) == 0) { 
                return false; 
        } 

        $pString = str_replace(" ", "", $ptString); 

        if (substr_count($pString, ",") > 1) 
            $pString = str_replace(",", "", $pString); 

        if (substr_count($pString, ".") > 1) 
            $pString = str_replace(".", "", $pString); 

        $pregResult = array(); 

        $commaset = strpos($pString,','); 
        if ($commaset === false) {$commaset = -1;} 

        $pointset = strpos($pString,'.'); 
        if ($pointset === false) {$pointset = -1;} 

        $pregResultA = array(); 
        $pregResultB = array(); 

        if ($pointset < $commaset) { 
            preg_match('#(([-]?[0-9]+(\.[0-9])?)+(,[0-9]+)?)#', $pString, $pregResultA); 
        } 
        preg_match('#(([-]?[0-9]+(,[0-9])?)+(\.[0-9]+)?)#', $pString, $pregResultB); 
        if ((isset($pregResultA[0]) && (!isset($pregResultB[0]) 
                || strstr($preResultA[0],$pregResultB[0]) == 0 
                || !$pointset))) { 
            $numberString = $pregResultA[0]; 
            $numberString = str_replace('.','',$numberString); 
            $numberString = str_replace(',','.',$numberString); 
        } 
        elseif (isset($pregResultB[0]) && (!isset($pregResultA[0]) 
                || strstr($pregResultB[0],$preResultA[0]) == 0 
                || !$commaset)) { 
            $numberString = $pregResultB[0]; 
            $numberString = str_replace(',','',$numberString); 
        } 
        else { 
            return false; 
        } 
        $result = (float)$numberString; 
        return $result; 
} 
?> 

它适用于我列表中的几乎所有数字。由于我使用丹麦克朗作为货币,一些数字的格式如下:Kr。 100.00。我解决了这个问题,只需将这些行放在 parseFloat 函数的顶部即可。

$prefix = substr($ptString, 0, 4); 
if ($prefix == "kr. " || $prefix == "Kr. ")
    $ptString = substr($ptString,4);

$prefix = substr($ptString, 0, 3); 
if ($prefix == "Kr." || $prefix == "kr.")
    $ptString = substr($ptString,3);

现在我的问题只出现在数字上,格式如下 12.123.00,应该是 12123.00

认为这可以用正则表达式解决(不是我的强项)。

基本上,我要求将 xx.xxx.dd 转换为 xxxxx.dd。

不要考虑舍入问题。

I have to read a rather large file, containing numbers, formatted in different ways.

I have tried using the builtin floatval() function. This function works on some numbers, such as 22000.76, but not 22000,76.

Reading the comments on php.net have helped me a lot, I found this parseFloat function:

<?php 
function parseFloat($ptString) { 
        if (strlen($ptString) == 0) { 
                return false; 
        } 

        $pString = str_replace(" ", "", $ptString); 

        if (substr_count($pString, ",") > 1) 
            $pString = str_replace(",", "", $pString); 

        if (substr_count($pString, ".") > 1) 
            $pString = str_replace(".", "", $pString); 

        $pregResult = array(); 

        $commaset = strpos($pString,','); 
        if ($commaset === false) {$commaset = -1;} 

        $pointset = strpos($pString,'.'); 
        if ($pointset === false) {$pointset = -1;} 

        $pregResultA = array(); 
        $pregResultB = array(); 

        if ($pointset < $commaset) { 
            preg_match('#(([-]?[0-9]+(\.[0-9])?)+(,[0-9]+)?)#', $pString, $pregResultA); 
        } 
        preg_match('#(([-]?[0-9]+(,[0-9])?)+(\.[0-9]+)?)#', $pString, $pregResultB); 
        if ((isset($pregResultA[0]) && (!isset($pregResultB[0]) 
                || strstr($preResultA[0],$pregResultB[0]) == 0 
                || !$pointset))) { 
            $numberString = $pregResultA[0]; 
            $numberString = str_replace('.','',$numberString); 
            $numberString = str_replace(',','.',$numberString); 
        } 
        elseif (isset($pregResultB[0]) && (!isset($pregResultA[0]) 
                || strstr($pregResultB[0],$preResultA[0]) == 0 
                || !$commaset)) { 
            $numberString = $pregResultB[0]; 
            $numberString = str_replace(',','',$numberString); 
        } 
        else { 
            return false; 
        } 
        $result = (float)$numberString; 
        return $result; 
} 
?> 

It works on almost all the numbers in my list. As I am using DKK as currency, some numbers are formatted like this: Kr. 100.00. This problem, I solved just be putting these lines in to top of the parseFloat function.

$prefix = substr($ptString, 0, 4); 
if ($prefix == "kr. " || $prefix == "Kr. ")
    $ptString = substr($ptString,4);

$prefix = substr($ptString, 0, 3); 
if ($prefix == "Kr." || $prefix == "kr.")
    $ptString = substr($ptString,3);

Now my problem only occurs on numbers, formatted like this 12.123.00, which should be to 12123.00

I think this can be solved with regular expressions (not my strength).

Basically, I'm asking to convert xx.xxx.dd to xxxxx.dd.

Dont think about rounding issues.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一页 2024-11-23 15:45:34

NumberFormatter 有一个 parseCurrency( ) 可能有用的方法。

NumberFormatter has a parseCurrency() method that may prove useful.

紫瑟鸿黎 2024-11-23 15:45:34

如果您的所有问题都是从 xx.xxx.dd 转换为 xxxxx.dd,则无需使用正则表达式引擎,您可以只使用explode() 函数,例如:

$n = "123.456.78";
$a = explode(".", $n);

if(sizeof($a)==3) //had more than one dot
  $n = $a[0].$a[1].".".$a[2];

//$n now is 123456.78

If all your trouble is converting from xx.xxx.dd to xxxxx.dd, no need to use the regexp engine, you can just use the explode() function, for example:

$n = "123.456.78";
$a = explode(".", $n);

if(sizeof($a)==3) //had more than one dot
  $n = $a[0].$a[1].".".$a[2];

//$n now is 123456.78
故事灯 2024-11-23 15:45:34

如果数字总是有分数(便士、美分等)..尝试这个技巧:

$value = (float)preg_replace('/\D/', '', $strToParse)/100;

if the figures always have the fraction (pence, cents, etc).. try this hack:

$value = (float)preg_replace('/\D/', '', $strToParse)/100;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文