PHP number_format error

Hypister

I have a string like:

R$10,25

I need to convert it to:

10.25

I'm trying with:

$value = 'R$10,20';
$value_subs = str_replace(array('R', '$'), '', $value);
$value_formated = number_format($value_subs, 2, ',', '.');

After I will compare the $value_formated value with another to proceed the code, like:

$another_value = 20.40;
if($value_formated <= $another_value)
{
    //Error
}
else
{
    //Proceed
}

But I receive the error A non well formed numeric value encountered.
How to achieve this? Having in mind that I receive the value of the $value variable from the client, so, anything can be present here, but I just will pass it if aren't according with if($value_formated <= $another_value).

roullie

combining @Hayden Schiff 's answer

<?php

    $value = 'R$10,20';
    $value_subs = str_replace(array('R', '$',','), array('','','.'), $value);
    $value_formated = floatval($value_subs);

    $another_value = 20.40;
    if($value_formated <= $another_value)
    {
        echo "Less or equal";
    }
    else
    {
        echo "Greater";
    }

replace , to a . then parses the result to a float

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related