Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP NumberFormatter removing the .00

Tags:

php

Trying to remove the last .00 from a currency formatted using PHP NumberFormatter but it doesn't seem possible. I can see this option but it doesn't seem to affect a currency: 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

I would do a str_replace for .00 but this may be different depending on locale so it doesn't seem that easy.

like image 398
Intellix Avatar asked Oct 21 '22 06:10

Intellix


1 Answers

You can force that only with the format() method:

$nf = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY);
$nf->setTextAttribute(\NumberFormatter::CURRENCY_CODE, 'EUR');
$nf->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 0);
echo $nf->format(1);
like image 112
pozs Avatar answered Nov 03 '22 04:11

pozs