2011-10-01 131 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);