How to add values in php? [duplicate]

2

Good morning,

I want to add values to a form, but I can not. I need to do the following calculation:

<?php
vlMateriaPrima = 3,05;
$isumosMateriais = 0,25;
$embalagem = 0,50;

$custoDireto = vlMateriaPrima + $isumosMateriais + $embalagem;
?>

These values must be filled in with a comma in the form. The result is to be 3.80 and only 3 comes out

    
asked by anonymous 13.09.2016 / 16:07

1 answer

2

The decimals in PHP are separated by . (dot) and there are no thousands separators.

Do this:

<?php
vlMateriaPrima = 3.05;
$isumosMateriais = 0.25;
$embalagem = 0.50;

$custoDireto = vlMateriaPrima + $isumosMateriais + $embalagem;
?>

If you receive formatted data by separating decimals with a comma, you can do this:

<?php
function moedaPhp($str_num){
    $resultado = str_replace('.', '', $str_num);
    $resultado = str_replace(',', '.', $resultado);
    return $resultado;
}

echo moedaPhp('12.654,56'); // retorna: 12654.56
    
13.09.2016 / 16:14