Alex Rivera | Logout

Formatting price as comma separated

Asked 2012-07-25T12:37:11.057
10

In my database I have values like

256.23, 200.33, 89.33, 133.45,

I have to multiply these values with thousand and then format the result as price(comma separated)

256.23 x 1000 = 256230            I want to show this as            256,230

200.33 x 1000 = 200330            I want this as                    200,330

89.33  x 1000 = 89330             I want this as                    89,330

Currently I am using formula

echo "Price is : $".$price*1000;

But how to format this, I've no idea.

Edit
Report

2 Answers

33

You are looking for the number_format function.

$price=123456;
echo number_format($price);
// output: 123,456

This function accepts either one, two, or four parameters (not three):

If only one parameter is given, number will be formatted without decimals, but with a comma (",") between every group of thousands.

If two parameters are given, number will be formatted with decimals decimals with a dot (".") in front, and a comma (",") between every group of thousands.

If all four parameters are given, number will be formatted with decimals decimals, dec_point instead of a dot (".") before the decimals and thousands_sep instead of a comma (",") between every group of thousands.

answered 2012-07-25T12:38:50.087
2

Check number_format, here is an example

echo number_format(8333*1000, 3, ',', '.');
answered 2012-07-25T12:41:13.003

Your Answer