PHP 浮点数输出格式

PHP float output format

我在 php 中遇到了一些奇怪的行为,在某些时候 PHP 开始用逗号而不是点输出浮点值。

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

而且,正如预期的那样,所有带有来自 PHP 的浮点数(价格、数量等)的跟踪脚本现在都不起作用。

尚未找到任何 php 或 apache 设置来控制它。有没有人遇到过这个?也许知道如何 fix/change 那?

P.S。为 LC_ALL 和 LC_NUMERIC 类别尝试了 setlocale(),但不起作用。

您可以使用 number_format 始终保持清洁。

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

for more

这是因为本地化。从 PHP 5.3.0 开始,您的 php.ini 中有一个 intl.default_locale 选项。当您将其更改为使用您想要的符号的国家/地区时应该可以工作。

来自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.

Germany and USA

的示例

正如您在数字 table 下看到的那样:

德国:

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

美国:

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

编辑:

...as its effective value depends on the server's environment.

这意味着您必须更改服务器环境中的本地。当没有设置服务器本地时,它只使用php.ini的设置值。

对于Linux:

如果要更改或设置本地系统,请使用 update-locale 程序。 LANG 变量允许您设置整个系统的语言环境。

以下命令将 LANG 设置为 en_US.ISO8859-1 并删除 LANGUAGE.

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

您可以在以下文件中找到全局区域设置:

  • /etc/default/locale – 在 Ubuntu/Debian
  • /etc/locale.conf – 在 CentOS/RHEL