Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an integer to a float in PHP rounds the float to 1 decimal place

For example, I have this line of code:

echo 10359023529 + 0.2137582935;

Why does this return:

10359023529.2

instead of:

10359023529.2137582935

like image 978
James Simpson Avatar asked Oct 19 '10 01:10

James Simpson


People also ask

How do you round to one decimal place in PHP?

echo 'Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision' . PHP_EOL; var_dump(round( 1.55, 1, PHP_ROUND_HALF_DOWN)); var_dump(round(-1.55, 1, PHP_ROUND_HALF_DOWN));

How do I round a float number in PHP?

The round() function rounds a floating-point number. Tip: To round a number UP to the nearest integer, look at the ceil() function. Tip: To round a number DOWN to the nearest integer, look at the floor() function.

How do I round to 2 decimal places in PHP?

Example #1 round() examples php echo round(3.4); // 3 echo round(3.5); // 4 echo round(3.6); // 4 echo round(3.6, 0); // 4 echo round(1.95583, 2); // 1.96 echo round(1241757, -3); // 1242000 echo round(5.045, 2); // 5.05 echo round(5.055, 2); // 5.06 ?>


1 Answers

Use: bcadd()

echo bcadd(10359023529, 0.2137582935, 10);
like image 75
Ruel Avatar answered Sep 23 '22 01:09

Ruel