Reputation: 1
I am trying to round down numbers using PHP.
I have managed to do this if the value has a decimal place, using this method below.
$val = floor($val * 2) / 2;
echo 'hello'. $val;
If the value I am trying to round down doesn't have a decimal place and the above code is not working.
The values I am trying to round down.
32456 => 32000
4567 => 4000
38999 => 38000
Upvotes: 0
Views: 1207
Reputation: 44851
There are a few ways to do this. The most common way (for rounding down to the nearest 1000) would be something like this:
function roundDown1000($n)
{
return floor($n / 1000) * 1000;
}
More generally:
function roundDown($n, $increment)
{
return floor($n / $increment) * $increment;
}
If you wanted, you could also do $n - ($n % 1000)
, but this will get weird results for $n < 0
.
Upvotes: 5