3

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.

mskfisher
  • 3,291
  • 4
  • 35
  • 48
Ragnar123
  • 5,174
  • 4
  • 24
  • 34

3 Answers3

2

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

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • As I do not have `NumberFormatter` on my php installation, this is not an option, though it would be the best solution. Do you think it could be solved in regular expressions? – Ragnar123 Jun 22 '11 at 13:21
1

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

$value = (float)preg_replace('/\D/', '', $strToParse)/100;
Oliver O'Neill
  • 1,229
  • 6
  • 11
1

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
danii
  • 5,553
  • 2
  • 21
  • 23