Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting price as comma separated

Tags:

php

formatting

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.

like image 615
Leo Avatar asked Jul 25 '12 12:07

Leo


People also ask

How can I format a string number to have commas?

For format String "%,. 2f" means separate digit groups with commas and ".

What is a thousand separator?

The character used as the thousands separator In the United States, this character is a comma (,). In Germany, it is a period (.). Thus one thousand and twenty-five is displayed as 1,025 in the United States and 1.025 in Germany. In Sweden, the thousands separator is a space.


1 Answers

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.

like image 165
Fluffeh Avatar answered Oct 26 '22 06:10

Fluffeh