Talk to float ... problem

Asked

Viewed 52 times

-1

I am trying to convert the string value '1234,56' float, but I found this way of converting number but comes only in integer. In which way I can bring the values in float ?

Result looks like this : float 1234

But I need the values after the comma...

$val_orcamento = $this->request->post['valorca'];
				$quantidade = $this->request->post['quantidade'];
				$valor_unit = $this->request->post['valor'];
			 
				$varquantidade[0] = ((float)$quantidade[0]*1.00);
				$varunit[0] = ((float)$valor_unit[0]*1.00);

1 answer

3

The main problem is: PHP treats float value using '.', not ','. That is, if you ask for PHP to convert "1234.56" to float, it will read up to "1234", find a comma and ignore that". To convert a number with a comma to float, treat it as follows:

$number = "12345,67";
$float = (float) str_replace(',', '.', $number);

Ready. That alone solves the problem of your number not being treated correctly as float, because for PHP it really is not a float! =)

  • Thank you very much saw , it worked here hehe , I was so involved in the code that I forgot that PHP reads only point :)

Browser other questions tagged

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