Home >Backend Development >PHP Tutorial >How to Round Decimals Down to Two Decimal Places in PHP?
Rounding Decimals Down to Two Places in PHP
In PHP, rounding down a decimal to two decimal places can be challenging. While functions like number_format() round to the nearest point, methods like substr() fail to account for variable digit counts.
To achieve this rounding precision, consider the following solution:
<code class="php">$number = 49.955; $rounded = floor($number * 100) / 100; echo $rounded; // Output: 49.95</code>
Explanation:
This method ensures accurate rounding down to two decimal places, regardless of the input number's starting value.
The above is the detailed content of How to Round Decimals Down to Two Decimal Places in PHP?. For more information, please follow other related articles on the PHP Chinese website!