PHP round(12,2)显示12而不是12.00

I am currently running on latest version of PHP, OS is ubuntu. I used round() in PHP, but got a result which is not expected of normal one, because on rounding 12 or any integer with any other integer value there should be the number of zeros added at the end after decimal such as for round(34,3) the result should be 34.000 but it is displaying 34. How to get the result with floating points for integers?

round returns a numeric value. 12.00 is a string value (the numeric version would strip leading/trailing zeroes).

Use number_format(12,2) instead.

Round expects to receive a a float not an int. If provided a int it will just be returned.

If you need to round and then display al numbers with 2 decimal places pass through round then through number_format

round will round the number, but it won't covert it to a string, so the precision that gets output when you print it is still infered from the number itself.

You can use sprintf to format it as a string at whatever precision you like.

$number = 12;
$number_as_string = sprintf("%0.2f", $number);
print $number_as_string;