PHP 货币格式

PHP Money Fomat

我已经有了带小数的完整 int 值,我想在 PHP

中像这样转换
500 -> 5,00$
 5070 -> 50,70$
 50070 -> 500,70$
 500000 -> 5.000,00$

所以格式应该是这样的xxx.xxx.xxx.xxx,xx$

有什么功能吗?

number_format() 可以胜任。

它是这样工作的:string number_format(float $number, int $decimals = 0, string $dec_point = ".", string $thousands_sep = ",")

number

The number being formatted.

decimals

Sets the number of decimal points.

dec_point

Sets the separator for the decimal point.

thousands_sep

Sets the thousands separator.

在你的情况下,可能是 $myMoney = number_format($cents / 100, 2, ",", ".") . "$";

我看到你的货币格式是美分,所以你可以这样做:

$prices = [
    500,
    5070,
    50070,
    500000
];
foreach ($prices as $price) {
    echo money_format("Price: %i", $price / 100);
}