将货币格式的字符串转换为浮点数
我必须读取一个相当大的文件,其中包含以不同方式格式化的数字。
我尝试使用内置的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NumberFormatter
有一个parseCurrency( )
可能有用的方法。NumberFormatter
has aparseCurrency()
method that may prove useful.如果您的所有问题都是从 xx.xxx.dd 转换为 xxxxx.dd,则无需使用正则表达式引擎,您可以只使用explode() 函数,例如:
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:
如果数字总是有分数(便士、美分等)..尝试这个技巧:
if the figures always have the fraction (pence, cents, etc).. try this hack: