I have a price, let's say $price = 9,00 €
. If price have 2 zeroes after comma is there easy way to transform it to 9 €
? I mean is there a function for that or something. If that is not possible with €
character, how to do that only with integers 9,00
> 9
, but 9,20
would stay 9,20
.
I know it could be done with substr
or some other ways, but I'd like to know if PHP have function for this situation.
You have to use NumberFormatter.
$formatter = new NumberFormatter('de-DE', NumberFormatter::CURRENCY);
parse it to float
$value = $formatter->parseCurrency('9,00 €', 'EUR');
check if is int or float
$digits = floor($value) == $value ? 0 : 2;
reformat
$formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $digits);
echo $formatter->formatCurrency($value, 'EUR');