Error in variable value

2

I have the following doubt, I have to add a set of values that are set to string's , so I had to convert to float , I used the function str_replace() , except that when testing the variable with var_dump and perform the sum in a function that runs a array with the objects of the variables, I noticed that the original value, in case R $ 4,200.00, was converted to (4,2) literally.

How do I resolve this issue? Because I need to add the value of 4,200.00. The output on the web is as follows:

  

1 Microsoft Optical Mouse R $ 89,90 Peripherals
  2 HP LaserJet 1320 Printer - Compare Prices and Buy at PriceGrabber   3 Macbook Pro Apple R $ 4,200.00 Notebooks
  4 Wireless Keyboard Genius R $ 110,00 Peripherals
  5 Lenovo Computer R $ 2.100,00 Computers

     

89.90float (89.9)
  850.00float (850)
4.200.00float (4.2)

asked by anonymous 07.05.2015 / 03:53

1 answer

2

The problem is in format. A float value has its decimal part separated by a point, and the thousand part has no separation.

Its value is 4.200,00 . You change the comma by dot by getting 4.200.00 . Since there is a point between 4 and 2 (thousands), the language sees this as the decimal part, simplifying to 4.2 .

So you should replace point by empty and then comma by point:

$charRemove = array("R", "$", ".", ",");
$charSub = array("", "", "", ".");
    
07.05.2015 / 13:27