Show a number to two decimal places in PHP

Create a CSV File for a user in PHP
How to generate a random, unique, alphanumeric string in PHP?

What’s the correct way to round a PHP string to two decimal places?

solution 1.

You can use number_format()

return number_format((float)$number, 2, '.', '');

Example

$foo = "105";
echo number_format((float)$foo, 2, '.', '');  // Outputs -> 105.00

This function returns a string.

solution 2.

echo round(520.34345, 2);   // 520.34
echo round(520.3, 2);       // 520.3
echo round(520, 2);         // 520

Use round() (use if you are expecting a number in float format only.

Create a CSV File for a user in PHP
How to generate a random, unique, alphanumeric string in PHP?

Stay up-to-date about PHP!

We don’t spam!

en English
X
Scroll to Top