skip to Main Content

I’ve encountered some strange behavior in php, at some point PHP started to output float values with comma instead of dot.

<?php
echo 12.3;
//outputs "12,3" to both browser and CLI

And, as expected, all tracking scripts with float numbers from PHP (prices, QTY etc) are not working now.

Haven’t found any php or apache setting to control that. Has anyone met this before? And maybe knows how to fix/change that?

P.S. Tried setlocale() for LC_ALL and LC_NUMERIC categories, doesn’t work.

2

Answers


  1. You could use number_format to always keep it clean.

    number_format(12.20, 2, '.') : string
    

    for more

    Login or Signup to reply.
  2. It’s because of localization. Since PHP 5.3.0 there is a intl.default_locale option in your php.ini. Should work when you change it to a country, that uses your wanted notation.

    Explanation from php.net:

    The locale that will be used in intl functions when none is specified (either by >omitting the corresponding argument or by passing NULL). These are ICU locales, not >system locales. The built-in ICU locales and their data can be explored at ยป >http://demo.icu-project.org/icu-bin/locexp.

    The default value is empty, which forces the usage of ICU’s default locale. Once set, >the ini setting cannot be reset to this default value. It is not recommended that this >default be relied on, as its effective value depends on the server’s environment.

    Example for Germany and USA

    As you can see under numbers table:

    Germany:

    Number Pattern: #,##0.### 1.234,56

    USA:

    Number Pattern: #,##0.### 1,234.56

    Edit:

    …as its effective value depends on the server’s environment.

    This means you have to change the local on your server environment. It only uses the set value of php.ini when no server local is set.

    For Linux:

    If you want to change or set system local, use the update-locale program. The LANG variable allows you to set the locale for the entire system.

    The following command sets LANG to en_US.ISO8859-1 and removes definitions for LANGUAGE.

    $ sudo update-locale LANG=LANG=en_US.ISO8859-1 LANGUAGE
    OR
    $ sudo localectl set-locale LANG=en_US.ISO8859-1
    

    You can find global locale settings in the following files:

    • /etc/default/locale โ€“ on Ubuntu/Debian
    • /etc/locale.conf โ€“ on CentOS/RHEL
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search