Question

I am getting a couple of values from a database query. The values that I am getting look like:

10.810000000000000497 and I want to use just '10.81'.

or

2.6899999999999999467 and I want to use just 2.69

when using ini_set('precision', 20);

How how should I use this values with bcadd() for example? As I just want to add 10.81 + 2.69?

Was it helpful?

Solution

The php BC_ functions has another option: scale.

$num1 = 10.810000000000000497;
$num2 = 2.6899999999999999467;
$result = bcadd($num1, $num2, 2);
echo $result; // 13.50

If you want to use it several times you may use bcscale() and such you won't need to specify the third option each time you use it:

bcscale(2);
$num1 = 10.810000000000000497;
$num2 = 2.6899999999999999467;
$result = bcadd($num1, $num2);
echo $result; // 13.50
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top