CancelImage Upload

Performing calculations to a specified decimal point in PHP

Here are four PHP maths functions that might come in handy. If for instance you have to perform a simple addition, subtraction, division or multiplication, and want the result to be rounded to a specific decimal point, here is how you can do it:

<?php

//For additions

echo "bcadd(16.01203234, 45.12032703, 4) = ".bcadd(16.01203234, 45.12032703, 4);

echo "<br><br>";

//For subtractions

echo "bcsub(16.01203234, 45.12032703, 4) = ".bcsub(16.01203234, 45.12032703, 4);

echo "<br><br>";

//For multiplications

echo "bcmul(16.01203234, 45.12032703, 4) = ".bcmul(16.01203234, 45.12032703, 4);

echo "<br><br>";

//For divisions

echo "bcdiv(16.01203234, 45.12032703, 4) = ".bcdiv(16.01203234, 45.12032703, 4);

echo "<br><br>";

?>

The results:

bcadd(16.01203234, 45.12032703, 4) = 61.1323

bcsub(16.01203234, 45.12032703, 4) = -29.1082

bcmul(16.01203234, 45.12032703, 4) = 722.4681

bcdiv(16.01203234, 45.12032703, 4) = 0.3548


Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com