PHP NumberFormatter 删除 .00

Int*_*lix 5 php

试图从使用 PHP NumberFormatter 格式化的货币中删除最后一个 .00,但似乎不可能。我可以看到这个选项,但它似乎不会影响货币:DECIMAL_ALWAYS_SHOWN

$nf = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY);
$nf->formatCurrency(0, 'EUR');
// Output is €0.00 but it doesn't seem possible to remove the .00
Run Code Online (Sandbox Code Playgroud)

我会为 .00 做一个 str_replace ,但这可能会因语言环境而异,所以看起来并不那么容易。

poz*_*ozs 6

您只能使用以下format()方法强制执行此操作:

$nf = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY);
$nf->setTextAttribute(\NumberFormatter::CURRENCY_CODE, 'EUR');
$nf->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 0);
echo $nf->format(1);
Run Code Online (Sandbox Code Playgroud)