Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: If number (with comma), convert it to right number format (with point)

I have an array of mixed values:

$row = array('Unspecified risk','Yes','8','3','2','13','none','-1,49','-2,51','-1,46','-1,54'); -1,94   -1,55

As you can see it contains text and both negative and positive comma-numbers.

I need to convert the numeric values to the right number format and leave the text values as is.

Now I'm looping over the values:

foreach ($row as $value) {
    // If $value is numeric, convert it to the 
    // right number format for use with MySQL (decimal(10,2))
    // If not, leave it be.
}

Two related questions I've investigated but cannot find a suitable solution.

  • Converting a number with comma as decimal point to float
  • I need php regular expression for numeric value including "-" and ","

Could anyone provide a practical example?

like image 974
chocolata Avatar asked Mar 12 '13 13:03

chocolata


People also ask

How can I get only 2 decimal places in PHP?

$twoDecNum = sprintf('%0.2f', round($number, 2)); The rounding correctly rounds the number and the sprintf forces it to 2 decimal places if it happens to to be only 1 decimal place after rounding. Show activity on this post. This will give you 2 number after decimal.

What is number format PHP?

The number_format() function is an inbuilt function in PHP which is used to format a number with grouped thousands. It returns the formatted number on success otherwise it gives E_WARNING on failure. Syntax: string number_format ( $number, $decimals, $decimalpoint, $sep )


1 Answers

You don't need to use regular expressions.

use str_replace() as you need to replace the ',' for a '.', and then use intval() or floatval() functions to get the numeric value. You can also use strstr() to look for the '.' and decide if using intval() or floatval()

Example:

$row = array('Unspecified risk', 'Yes', '8', '3', '2', '13', 'none', '-1,49', '-2,51', '-1,46', '-1,54');

    function toNumber($target){
        $switched = str_replace(',', '.', $target);
        if(is_numeric($target)){
            return intval($target);
        }elseif(is_numeric($switched)){
            return floatval($switched);
        } else {
            return $target;
        }
    }

    $row = array_map('toNumber', $row);

    var_dump($row);

We use str_replace() to replace the dot for the comma, this way it's a international notation float, even if it's on string, this way later on we can check if it's numeric with is_numeric() <-- this function is awesome as it detects from a string if it's a number or not, no matter integer or float etc.

We use the is_numeric to check if the value is integer float or text and return the corresponding value using intval() or floatval() (the value without the replace applied will not return as a valid numeric, only after switching the , and . it will return true as numeric).

We use $row = array_map('toNumber', $row); to apply the changes to the array.

Profit xD

like image 61
aleation Avatar answered Nov 15 '22 17:11

aleation