问题
In some circumstances PHP is changing the decimal separator after the operation, here is an example:
<?php
echo $amount; //21.960000
echo $this->obj_vat->vat; //10.00
$amount= $amount + ( $amount * ( $this->obj_vat->vat / 100 ) );
echo $amount; //24,156
?>
Why the decimal separator changes to ','?
This is a multilanguage website. In the spanish version the locale is set to spanish (es_ES), and the decimal separator is ','. That's why is changing.
A solution is to force the LC_NUMERIC to english:
setlocale( LC_ALL, $locale );
setlocale( LC_NUMERIC, 'en_GB' );
But I don't know if this is a proper solution.
回答1:
Internally, PHP stores the amount variable as a float, so the problem is when you display that value. Try this:
<?php
echo $amount; //21.960000
echo $this->obj_vat->vat; //10.00
$amount= $amount + ( $amount * ( $this->obj_vat->vat / 100 ) );
echo number_format($amount, 2, '.', ',');
See a demo here: http://sandbox.onlinephpfunctions.com/code/ba34855a2de309c842c7ce678713f4168a852999
回答2:
Every think is worked fine.
For your information do somthink like this,
<?php
echo $amount; //21.960000
echo $this->obj_vat->vat; //10.00
$amount= $amount + ( $amount * ( $this->obj_vat->vat / 100 ) );
echo (float) $amount; //24.156
?>
回答3:
This is a local configuration issue, probably caused after installing php-intl recently, which changed it for you unintentionally.
Edit your /etc/default/locale
file and change the LC_NUMERIC
line to
LC_NUMERIC = "en_GB.UTF-8"
You might want to change any other setting there, then reboot.
来源:https://stackoverflow.com/questions/41954111/php-change-the-decimal-separator