PHP change the decimal separator

不打扰是莪最后的温柔 提交于 2021-01-02 20:04:01

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!