【问题标题】:PHP number_format() FunctionPHP number_format() 函数
【发布时间】:2013-10-11 14:12:37
【问题描述】:

我正在尝试按以下方式格式化我的号码

12,34,56,789.00

我试过了

number_format($row{'money'},2,".",",")

我来了

123,456,789.00 

这不是我想要的。那么,如何做到这一点? (12,34,56,789.00)

【问题讨论】:

  • number_format() 方法格式化定义/标准组(即数千、数百万等)中的数字。您要求的格式,2 个数字 + 逗号 + 2 个数字 + 逗号,不是标准格式。您必须编写自己的函数来进行分组(可能使用money_format())。
  • 你不能用 number_format 做到这一点,因为函数的目的是明确地“格式化一个数以千计的数字”

标签: php


【解决方案1】:

您只需使用名为 money_format 的 PHP 函数即可完成此操作

$amount = '123456789.00';
setlocale(LC_MONETARY, 'en_IN');
$amount = money_format('%!i', $amount);
echo $amount; // 12,34,56,789.00

仅当系统具有 strfmon 功能时才定义函数 money_format()。例如,Windows 没有,因此 money_format() 在 Windows 中是未定义的。

所以你可以使用这个php代码:

setlocale(LC_ALL, ''); // Locale will be different on each system.
$amount = 123456789.00;
$locale = localeconv();
echo number_format($amount, 2, $locale['decimal_point'], $locale['thousands_sep']);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多