W3docs

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

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

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

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

$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:

Example of using PHP sprintf() function with a floating number

<?php

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