Calculation with decimal number PHP

Asked

Viewed 3,938 times

3

I’m making a query, which gets 2 values from the database. It returns the correct values with comma. But when multiplication is done, it is not counting the decimal places.

foreach ($compra['CompraItem'] as $valores) {                       
    $cotacao = $modeloMoedaCotacao->find('first', array('conditions'=>array(
        'MoedaCotacao.moeda_id' => $valores['moeda_id'],
        'MoedaCotacao.data' => date("Y-m-d"),
    ), array(
        'limit' => 1,
    )));
    echo $cotacao = FloatFromSQL($cotacao['MoedaCotacao']['cotacao']);
    echo '<br />';                                                
    echo $valor = FloatFromSQL($valores['valor']);
    echo '<br />';                                                
    $valorTotal = $valor * $cotacao;
    echo $valorTotal;
    echo '<br />';                                                
}  

Return:

5.14000 1980.00 = multiplication returns without using decimal place >> 9900

3.23000 160,00 = multiplication returns without using decimal place >>480

5.14000 70,00 = multiplication returns without using decimal place >>350

  • What is the DBMS behind and, more importantly, can tell the type of data being used to store these numbers?

2 answers

1

The problem is that we use the comma as the decimal separator , in PHP the decimal separator is the point .

That said you have N ways to solve your problem, since bring those numbers formatted with dot . from your database or convert in PHP

<?php
$a = "5,14000";
$b = "1980,00";

$a = floatval(str_replace(",", ".", $a));
$b = floatval(str_replace(",", ".", $b));

var_dump($a*$b);

If you are using PHP in version larger than 5.3 and have the intl installed can use the Numberformatter.

  • Correctly, I just needed to convert them to "." because php only does multiplication with ".". Thank you very much.

  • 2

    The correct is to say that in mathematics, not PHP, the decimal separator is the point. The comma, is used in Brazil and other countries by bad habit and with the weather became custom.

1


If you will work with calculations often advise using a function that can format these values.

Example:


function valor_func($aValor, $aTipo, $a_dec = 2) { // valores com mascara e sem mascara
   switch ($aTipo):
      case 'C':// com mascara
         $valor = number_format(round($aValor, $a_dec), $a_dec, ',', '.');
         break;

      case 'S':// sem mascara
         $valor = str_replace(',', '.', str_replace('.', '', $aValor));
         break;

      case 'A':// arrendonda
         $valor = round($aValor, $a_dec);
         break;
      case 'D':// Decimais sem arredonda,sem mascara
         $posPonto = strpos($aValor, '.');
         if ($posPonto > 0):
            $valor = substr($aValor, 0, $posPonto) . '.' . substr($aValor, $posPonto + 1, $a_dec);
         else:
            $valor = $aValor;
         endif;
         break;
   endswitch;
   return $valor;
}

to display the values for the user use the type 'C' and to calculate use the type 'S' example :


$a = "5,14000"; // com mascara

echo valor_func($a,'S');// 5.14000 sem mascara
echo valor_func($a,'C');// 5,14000 com mascara

  • avoid this kind of multipurpose function. Something else that number_format (http://php.net/manual/en/function.number-format.php) would also help to format the number

  • I would like you to explain to me the "avoid multipurpose function", number_format is used in the function described above, this function besides adding routines of formatting to numbers it facilitates the use of the same.

Browser other questions tagged

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