Divide values in Brazilian currency (R $) in php?

2

I need to divide the value 6999.99 by 3, but when dividing it returns 2333.00 or does not return the cents, a hypothetical example follows:

$parcelas = 3;
$valor = double(6.999,99); //já usei com e sem double e na mesma
$valorTotal = number_format($valor, 2, '.', '');
$valor_parcela = $valorTotal / $parcelas;
echo 'R$'. number_format($valor_parcela, 2, ',', '.');

The value is already formatted from the input with a javascript mask, if you pass the value of this form 6999.99, it is printed correctly. Thanks

    
asked by anonymous 16.01.2017 / 19:05

2 answers

2

You need to treat the input value 6.999,99 to 6999.99

$valor = str_replace(',', '.', str_replace('.', '', '6.999,99'));
    
16.01.2017 / 19:22
3

The error is in this line $valor = double(6.999,99); should be $valor = 6999.99; , that is, it should be put in place of the . (dot) , (comma) .

<?php    
    $parcelas = 3;
    $valor = 6999.99; 
    $valorTotal = number_format($valor, 2, '.', '');
    $valor_parcela = $valorTotal / $parcelas;
    echo 'R$ '. number_format($valor_parcela, 2, ',', '.');

Example OnLine

16.01.2017 / 19:20