user3541142
user3541142

Reputation: 3

I'd like to format a record containing a number with a comma

I'd like the format of a record containing a number to be displayed with a comma separating every 1,000. The value in the record is the total price of a product.

In a very simplistic format:

$strSQL = "SELECT * FROM table WHERE model='A'";

$rs = mysql_query($strSQL);

while($row = mysql_fetch_array($rs)) {

echo $row['totalprice'] ;

The 'totalprice' record in the table for this example model contains the amount 32830

So the current output is 32830. The desired output I'd like is 32,830

I can't hard code the number in the code as there are many products to display with different costs.

Upvotes: 0

Views: 39

Answers (1)

enapupe
enapupe

Reputation: 17029

http://php.net/number_format

string number_format ( float $number , int $decimals , string $dec_point , string $thousands_sep )

So, what you want is:

echo number_format($row['totalprice']);

Because:

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

Upvotes: 3

Related Questions