How to round a number to two decimal places
Short answer
The second argument of round() sets the number of decimal places:
<?php
echo round(3.14159, 2);Output:
3.14Always two digits
round() returns a number, so trailing zeros are not printed. To always show two digits, use number_format():
<?php
echo round(2.5, 2), "\n";
echo number_format(2.5, 2);Output:
2.5
2.50Down or up
To round in a chosen direction, multiply by 100, apply floor() or ceil() and divide back:
<?php
echo floor(3.789 * 100) / 100, "\n";
echo ceil(3.781 * 100) / 100;Output:
3.78
3.79