How to convert 0.5 to 0.50 in php
5 answers
Do
$x = 0.5;
printf("%.2f", $x);
Receive:
0.50
Explanation:
printf()
and sprintf()
can print / return a formatted string. There are many options options . Here I have used %.2f
. Let's take this separately:
-
%
denotes a placeholder to be replaced with$x
-
.
denotes a precision specifier -
2
belongs to the precision specifier - the number of digits after the decimal point -
f
- type specifier, here float as what we pass
As an alternative:
Use number_format()
:
echo number_format($x, 2);
Here 2
denotes the number of decimal digits you want in the output. You don't actually need to specify the third and fourth parameters as their default values ββare exactly what you want.
+5
source to share