Programming in PHP is not exactly easy, but it is also not hard if one thinks of a solution. How do we truncate a decimal value number to a number we would like? PHP does has a round() and number_format() function which do help in setting the desired amount of decimal number required.
Sometimes we would not have written functions available, thus manual calculations are needed in order to resolve this issue. TechieDan wishes that php would have more functions in terms of truncation like in Java, but this is all up to the community.
$a = 11.5468; $b = round($a,2); // this will return 11.55 $b = number_format($a,2); // this will also return 11.55
Now what if you wanted to return 11.54?
It seems like there are no such thing as roundDown like the one we have in Java.
So we have to do a truncation instead. Sadly, there is also no truncate function in php.
Refer below for the solution.
$a = 11.5468; // if you wanted to truncate to nearest 2 decimals, use floor with desired amount of 0. $b = floor($a * 100) / 100; // this will return 11.54 // if want to return 11.546 $b = floor($a * 1000) / 1000;
Your question has now been answered!