I have prices stored to five decimal places of precision, such as:
1.95000
2.25000
0.01150
2.10000
2.00000
When displaying prices, I'd like to show the standard $X.XX
format if the rest of the digits are just zero, but if there are significant digits then I don't want to cut them out (so I can't simply use number_format()
).
As an example, the above prices should display as:
1.95
2.25
0.0115
2.10
2.00
This process has to be done on hundreds of prices per page. What's an efficient way of formatting numbers in this manner?