Search code examples
phpnumber-formatting

PHP, Format Number to have always have 2 decimal places


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

Solution

  • You should use number_format():

    number_format(1.1, 2);