00 Votes

PHP: Output Number with X Decimal Places

Question by Guest | 2015-04-23 at 16:53

I am calculating some percentages using PHP in order to display them under each other within a table.

Of course, the resulting values are differing in the amount of decimal places. However, I would like to show them all with the same fixed number of decimal places.

Is there any function in PHP for this? Finally, it is looking messy when there are the numbers 3.5, 1.23 and 2.74818 next to each other.

ReplyPositiveNegative
0Best Answer0 Votes

You can simply use the PHP function number_format for this.

Here is a small example:

$k = 1.2345;
echo number_format($k, 2);
// output: 1.23

As a first parameter, you can pass the number, as second parameter, the number of decimal places.

More information about it and how to adjust the decimal separator to your needs you can learn here.
2015-04-25 at 21:58

ReplyPositive Negative
Reply

Related Topics

PHP: Rounding Numbers

Tutorial | 0 Comments

PHP: Current Date and Time

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.