I would like to calculate an integer part of division. The numerator and denominator (especially their precision) should not be altered because it might change from one calculation to other, also the denominator is as an example, its integer part as well as decimal part might be different.
I tried to use floor, ceil, round but none of them produced a correct result. Please see the code below, perhaps you'll spot the error:
<?php
$valueArr = [
// should return 1999
199.90,
199.92,
199.95,
199.97,
// should return 2000
200.00,
200.02,
200.05,
200.07,
// should return 2001
200.10,
200.12,
200.15,
200.17,
];
$denominator = 0.1;
$resultArr = [];
foreach ($valueArr as $value) {
$key = (string) $value;
$result = floor($value / $denominator);
$resultArr[$key] = $result;
}
echo "Denominator:\n";
var_dump($denominator);
echo "\n";
print_r($resultArr);
that gives result:
Denominator:
float(0.1)
Array
(
[199.9] => 1999
[199.92] => 1999
[199.95] => 1999
[199.97] => 1999
[200] => 2000
[200.02] => 2000
[200.05] => 2000
[200.07] => 2000
[200.1] => 2000
[200.12] => 2001
[200.15] => 2001
[200.17] => 2001
)
where:
[200.1] => 2000
is wrong because integer part of (200.1 / 0.1) is 2001.
Do you know how to produce correct result for the $valueArr
as above? What did I do wrong?
I'm using PHP 7.3.8 (cli)