tags:

views:

33

answers:

1

Hi,

I store money values in my db table. E.g. I have 2.50. But when I print that value the 0 is always missing so I get 2.5. The db table money field has the following type: decimal(6,2)

any idea how to fix that?

+6  A: 

By default PHP echo and print don't print the trailing zeros of a floating point number.

To overcome this you need to use printf as:

$money = 2.50;

printf("%.2f",$money); // prints 2.50

echo $money;           // prints 2.5
print $money;          // prints 2.5

The format specifier used in printf is "%.2f" what it means is always print atleast two digits after the decimal point and if there are not so many digits use 0.
Note that it is atleast two digits not equal to two digits. So if I print 1.234 with that format it will not truncate it to 1.23 but will print 1.234 and if I print 1 it will result in 1.00

codaddict
thanks, it works ;)
ArtWorkAD