Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Currency formatting trailing zeros

Is it possible to have PHP format currency values, for example: $200 will output as: $200 (without decimals) but with a number like $200.50 it will correctly output $200.50 instead of $200.5?

Thanks! :)

like image 296
TronCraze Avatar asked Feb 14 '11 18:02

TronCraze


2 Answers

$num_decimals = (intval($amt) == $amt) ? 0 :2;
print ("$".number_format($amt,$num_decimals);
like image 134
a1ex07 Avatar answered Sep 30 '22 06:09

a1ex07


If you don't mind handling the currency on your own, you can simply use the following on the number, to get the correct output.

This solution will always output trailing zeros (xx.x0 or xx.00 depending on the provided number)

$number = 1234
sprintf("%0.2f",$number);
// 1234.00
like image 37
Armand555 Avatar answered Sep 30 '22 04:09

Armand555