I want to drop off decimals without rounding up. For example if I have 1.505, I want to drop last decimal and value should be 1.50. Is there such a function in PHP?
echo(round(-4.40) . "<br>"); echo(round(-4.60));
The PHP number_format() function is used for formatting numbers with decimal places and thousands separators, but it also rounds numbers if there are more decimal places in the original number than required. As with round() it will round up on 5 and down on < 5.
You need floor() in this way:
$rounded = floor($float*100)/100;
Or you cast to integer:
$rounded = 0.01 * (int)($float*100);
This way it will not be rounding up.
Use the PHP native function bcdiv
echo bcdiv(2.56789, 1, 2); // 2.56
$float = 1.505;
echo sprintf("%.2f", $float);
//outputs 1.50
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With