Home » Php » PHP, Format Number to have always have 2 decimal places

PHP, Format Number to have always have 2 decimal places

Posted by: admin July 12, 2020 Leave a comment

Questions:

In PHP I have decimal numbers that may or may not have trailing zeros.

For example

 1
 1.1
43.87

How do I make sure that all these numbers have exactly two decimal places like this:

 1.00
 1.10
43.87
How to&Answers:

You should use number_format():

number_format(1.1, 2);