PHP float with 2 decimal places: .00
To format a float to have 2 decimal places in PHP, you can use the number_format() function.
To format a float to have 2 decimal places in PHP, you can use the number_format() function. For example:
Example of PHP float with 2 decimal places
php— editable, runs on the server
Info
Watch a video course: Learn object oriented PHP
Alternatively, you can use the printf() function with a floating point conversion specifier:
Example of using PHP printf() function with a floating point conversion specifier
php— editable, runs on the server
You can also use the ) function, which works the same way as printf(), but returns the formatted string instead of printing it:
Example of using PHP sprintf() function with a floating number
php— editable, runs on the server