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. For example:

<?php

$number = 123.456;
$formattedNumber = number_format($number, 2);
echo $formattedNumber; // Outputs 123.46

Watch a course Learn object oriented PHP

Alternatively, you can use the printf() function with a floating point conversion specifier:

<?php

$number = 123.456;
printf("%.2f", $number);  // Outputs 123.46

You can also use the ) function, which works the same way as printf(), but returns the formatted string instead of printing it:

<?php

$number = 123.456;
$formattedNumber = sprintf("%.2f", $number);
echo $formattedNumber;  // Outputs 123.46