【问题标题】:Formatting a number格式化数字
【发布时间】:2014-04-29 01:10:51
【问题描述】:

我有一张表格,上面有这样的数字:

19809.1
1982.0
1982.19

它的货币。有没有我可以用来格式化这些数字的函数?

19 809,10
1 982,00
1 982,19

【问题讨论】:

  • 看看number_format()
  • @Blaine:我一直觉得money_format()number_format() 更令人困惑。

标签: php number-formatting


【解决方案1】:

number_format 很容易做到这一点:

number_format(value, number_of_decimals, "decimal_separator", "thousand_separator")

类似这样的:

echo number_format(19809.1, 2, ",", " ");

这告诉数字有 2 位小数,逗号 , 作为小数分隔符,空格 作为千位分隔符。输出将是:

19 809,10

其他例子:

echo number_format(19809.1, 0, "", ".");
> 19.809
> no decimal and . as thousand separator

echo number_format(19809.1, 3, ".", ",");
> 19,809.100
> 3 decimals, comma as thousand separator and dot as decimal separator

【讨论】:

  • @ToKeN:<kbd> 元素/标签用于表示用户输入。这不是让链接看起来更漂亮或“样式化”您的帖子的方法。
  • @AmalMurali 没关系,看起来很漂亮
【解决方案2】:

DOCUMENTATION

<?php

$number = 1234.56;

// english notation (default)
$english_format_number = number_format($number);
// 1,235

// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56

$number = 1234.5678;

// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57

?>

【讨论】:

    【解决方案3】:

    来自http://www.php.net/manual/es/function.number-format.php

    // notación francesa
    $nombre_format_francais = number_format($number, 2, ',', ' ');
    // 1 234,56
    

    【讨论】:

      【解决方案4】:

      我会选择number_format函数:

      $val=array(19809.1, 1982.0, 1982.19);
      
      foreach ($val as $v)
       echo number_format($v, 2, ',', ' ');
                           _  _   _    _
                           ^  ^   ^    ^
                      value   |   |    |
              decimal positions   |    |
                  decimal separator    |
                     thousands separator
      

      返回:

      19 809,10
      1 982,00
      1 982,19
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-05
        • 2017-10-01
        • 2014-12-24
        • 1970-01-01
        • 1970-01-01
        • 2012-02-25
        • 2020-11-01
        • 1970-01-01
        相关资源
        最近更新 更多