将数字转换为货币


PHP Convert number to currency

我已经很好地寻找任何帮助我的问题,所以希望有人能在这里帮助我。

基本上我有一个保存到$price中的数字,这个数字是15900。应该是159.00。

有人知道怎么做吗?

使用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,这取决于国际化的符号和/或货币符号是否也很重要。

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