Archive for the ‘php’ Category

Displaying commas in php

Sunday, March 1st, 2009

For long, whenever I needed to format my figures (.. am talking of bucks), I had to write a new UDF to handle that. Just today when I was running through the php manual that I came across this neat function. — number_format()
Usage:
Supposing my variable that displays the currency where I need the thousand separator to be commas is $amount,

What I needed to do is to pass the variable as an argument, to the function and here is the code snippet that worked for me.

//other code lines

$amount =number_format($amount);

//other code lines
?>

It did the magic for me and when the amount was One million it was displayed like : 1,000,000

Have fun and enjoy as well!