Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting money formatted strings to floats

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.

like image 632
Ragnar123 Avatar asked Dec 16 '25 20:12

Ragnar123


1 Answers

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

like image 166
Ignacio Vazquez-Abrams Avatar answered Dec 19 '25 11:12

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!