Error in variable value

Asked

Viewed 113 times

2

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

How can I solve this problem? Because I need to add the value of 4,200.00. The output on the web was as follows :

1 Microsoft optical mouse R$ 89,90 Peripherals
2 HP Laser Printer 1320 R$ 850.00 Printers
3 Apple Macbook Pro $4,200 Notebooks
4 Genius wireless keyboard R$ 110,00 Perifericos
5 Lenovo Computer R$ 2,100.00 Computers

89.90float(89.9)
850.00float(850)
4.200.00float(4.2) <- Note here

The script is that of the class Produto:

<?php

class Produto {
 public $nome;
 private $codigo;
 private $valor;
 private $categoria;

 function __construct($nome,$codigo,$categoria,$valor){

     $this->nome=$nome;
     $this->categoria = $categoria;
     $this->codigo=$codigo;
     $this->valor=$valor;

 }

 function __toString(){

     return $this->codigo."&nbsp&nbsp&nbsp".
            $this->nome."&nbsp&nbsp&nbsp".
            $this->valor."&nbsp&nbsp&nbsp".
            $this->categoria->getNome()."&nbsp&nbsp&nbsp";

 }

 function getValor(){
     return $this->valor;
 }

 function calcularPreco($arrayProduto){
     $somaTotal=0;
     for($i=0;$i<=sizeof($arrayProduto)-1;$i++){

        $objProd=$arrayProduto[$i];

        $charRemove= Array("R","$",",");
        $charSub= Array("","",".");
        $novoValor= str_replace($charRemove,$charSub,$objProd->getValor());

        $somaTotal= $somaTotal += $novoValor; 
        echo $somaTotal."</br> R";
        echo $i."</br>";

            if($i==sizeof($arrayProduto)){
                $somaMedia=$somaTotal/$i;
            }else{
                echo $i."</br>";
            }

    }


 }
}
?>

And the class script that instantiates the object Produto:

<?php

    include "Categoria.class.php";
    include "Produto.class.php";
    // Instancia dos objetos de categoria.

    $cat1=new Categoria("Perifericos",1);
    $cat2=new Categoria("Notebooks",2);
    $cat3=new Categoria("Computadores",3);
    $cat4=new Categoria("Impressoras",4);

    //Array que guarda referencias de objetos categoria.
    $categoriaList= Array($cat1,$cat2,$cat3,$cat4);

    //Instnacia dos objetos produto.
    $prod1=new Produto("Mouse ótico Microsoft",1,$cat1,"R$ 89,90");
    $prod2=new Produto("Impressora HP Laser 1320",2,$cat4,"R$ 850,00");
    $prod3=new Produto("Macbook Pro Apple",3,$cat2,"R$ 4.200,00");
    $prod4=new Produto("Teclado sem fio Genius",4,$cat1,"R$ 110,00");
    $prod5=new Produto("Computador Lenovo",5,$cat3,"R$ 2.100,00");

    //Array que guarda referencias de objetos produto.
    $prodList= Array($prod1,$prod2,$prod3,$prod4,$prod5);

    //Percorrendo Array de categoria.
for($i=0;$i<=sizeof($categoriaList)-1;$i++){

    echo $categoriaList[$i]."</br>";

}


    //Percorrendo Array de produto.

for($i=0;$i<=sizeof($prodList)-1;$i++){

    echo $prodList[$i]."</br>";


}   

$objProd=$prodList[0];

$charRemove= Array("R","$",",");
$charSub= Array("","",".");
$novoValor= str_replace($charRemove,$charSub,$objProd->getValor());


echo $novoValor;

$doubString= (float) $novoValor;
var_dump($doubString);

echo "</br></br>";

$objProd=$prodList[1];

$charRemove= Array("R","$",",");
$charSub= Array("","",".");
$novoValor= str_replace($charRemove,$charSub,$objProd->getValor());


echo $novoValor;

$doubString= (float) $novoValor;
var_dump($doubString);

echo "</br></br>";

$objProd=$prodList[2];

$charRemove= Array("R","$",",");
$charSub= Array("","",".");
$novoValor= str_replace($charRemove,$charSub,$objProd->getValor());


echo $novoValor;

$doubString= (float) $novoValor;
var_dump($doubString);

echo "</br></br>";

//$prod1->calcularPreco($prodList);

?>

1 answer

2


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

Its value is 4.200,00. You exchange comma for dot getting 4.200.00. Since there is a point between 4 and 2 (thousand), the language sees it as the decimal part, simplifying to 4.2.

So you must replace point by blank and then comma by dot:

$charRemove = array("R", "$", ".", ",");
$charSub = array("", "", "", ".");
  • Dude but there’s another way? In Java I use dubbing :(

  • I don’t think so

Browser other questions tagged

You are not signed in. Login or sign up in order to post.