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.
Related Topics
Delphi/Lazarus: 3 Ways to round a Number to X Decimal Places
Tutorial | 7 Comments
PHP: Write Output of a Script into Variable
Tutorial | 0 Comments
PHP: Rounding Numbers
Tutorial | 0 Comments
PHP: Send Output of a Script by Mail
Tutorial | 0 Comments
PHP: Current Date and Time
Tutorial | 0 Comments
Delphi/Lazarus: Only allow Numbers in TEdit
Tip | 0 Comments
Lazarus: Input of binary, hexadecimal and integer numbers
Article | 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.
You can simply use the PHP function number_format for this.
Here is a small example:
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