2011-10-01 135 views
0

我已經很好的瞭解了我的問題,希望有人能幫助我。PHP將數字轉換爲貨幣

基本上我有一個數字,保存到$價格,數字是15900.其中應該翻譯爲159.00。

有誰知道如何做到這一點?

+1

'15931'會發生什麼,它是否也是'159.31'? – Shef

+0

將它作爲'15900'並視爲美分而不是美元。在津巴布韋,款待是百萬Z $,而不是千兆Z $。 –

+0

也可以在數字中顯示00嗎?例如159.00或164.78 – JackTheJack

回答

4

爲此使用number_format。它將返回一個字符串,從而保持小數位不變,即使它們是.00

$price = 15900; 

// Defaults to a comma as a thousands separator 
// but I've set it to an empty string (last argument) 
$formatted = number_format($price/100, 2, '.', ''); 

echo $formatted; 

或者更好的,也許看看money_format以及根據國際符號和/或貨幣符號是否是重要的爲好。

1
$current = 15900; 
$your = round($current/100, 2);