How to - convert mysql numbers into currency?

Soldato
Joined
7 Jan 2007
Posts
10,607
Location
Sussex, UK
I have a script below where I have ordered the top 3 rows by houseprice.

The trouble is how do I convert the houseprice, which prints as 150000 to 150,000?

I don't need to add the pound sign as I can call that in from a field in the mysql table. I just need to add in the commas automatically.

Any ideas?


PHP:
$result = mysql_query("SELECT * FROM table
                        ORDER by houseprice DESC
                        LIMIT 3"); 
            



while($row = mysql_fetch_array($result)){
    echo $row['road']. " - ".($row['houseprice']);
    echo "<br />";
}
 
You could also use money_format() with whatever locale you wanted to use, which you could pull from your DB too I guess. This is handy (if you ever wanted to do internationalisation of your script) because some countries probably format their currencies different to ours, i.e. commas/decimals reversed or the symbol at the end: 123.456,78€.

PHP:
setlocale(LC_MONETARY, 'en_GB');
echo money_format('%n', $row['houseprice']);


hehe this is exactly what I need to do.

The trouble is I want to select the top 5 houseprices which are going to be in $ £ and Euro, or maybe rand, SEK.

Any ideas?
 
Last edited:
Back
Top Bottom