Home > Article > Backend Development > How to Round Numbers to the Nearest 10 in PHP?
In PHP, you can round a number to the nearest 10 using the built-in round() function. However, if you want to round a number specifically to the nearest multiple of 10, there's a more precise approach than simply using round().
To round a number to the nearest 10, you can divide it by 10, apply the ceil() function to round up to the nearest integer, and then multiply the result by 10 again. This effectively eliminates the significant digits beyond the tens place.
<code class="php">$number = ceil($input / 10) * 10;</code>
Consider the number 23. Using the above approach:
<code class="php">$number = ceil(23 / 10) * 10;</code>
Therefore, the number 23 is rounded up to the nearest 10, resulting in 30.
The above is the detailed content of How to Round Numbers to the Nearest 10 in PHP?. For more information, please follow other related articles on the PHP Chinese website!