Error multiplying decimal by integer in php

Asked

Viewed 274 times

1

Follow the example of my code:

$v_quantidade = 10;
$v_preco = '3,68' ;
$v_total   = $v_preco * $v_quantidade;
echo "$v_total"; 

My variable $v_quantity it brings whole number, example: 3.. 5..10.. 20.etc. ...

My variable $price brings the price, example: 3.68.

When I do the calculation shown above is returning wrong,that example calculation ai is returning 10.

What I’m doing wrong ?

And if possible also another doubt together, some prices may come like this: 3,689 as I present in php only 3.68

  • Comma is not decimal separator.

  • 1
  • @rray Good morning, I take this information from the bank and it comes this way,how could I treat ? Any tips?

  • @Perfect rray, it worked. Just one more doubt,he returned like this: 36.8 how do I return so 36.80 ?

  • 1
  • 1
  • Thank you Diego F and rray, everything perfect.

Show 2 more comments

1 answer

2


Follow solution elaborated with the tips of @Diego F and @rray

  $v_quantidade = 10;
  $v_preco  = '3,68' ;
  $v_preco  = str_replace(',', '.', $v_preco);
  $v_numero = $v_quantidade * $v_preco; 
  $v_total  = number_format($v_numero, 2, ',', '.');
  echo "<p><b>VALOR TOTAL: R$ $v_total                  </br></p>";

Browser other questions tagged

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