【问题标题】:PHP number format without commaPHP数字格式不带逗号
【发布时间】:2013-06-10 16:48:36
【问题描述】:

我想像 1000.50 一样显示数字 1000.5,小数点后 2 位,没有逗号/千位分隔符。

我正在使用 number_format 来实现这一点:

number_format(1000.5, 2);

结果为 1,000.50。结果中不需要的千位附加逗号 (,) 分隔符。

如何显示带有尾随零且没有逗号的数字?

【问题讨论】:

    标签: php


    【解决方案1】:

    请参阅有关 number_format 的文档:http://php.net/number_format

    函数参数为:

    string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )

    所以使用:

    number_format(1000.5, 2, '.', '');
    

    这意味着您不使用任何(= 空字符串)千位分隔符,只使用小数点。

    【讨论】:

    • 谢谢!如果您稍后使用intval()floatval()doubleval() 转换格式化数字,则不包括逗号尤为重要。任何带有逗号的数字都将变为1!
    • 这个答案今天帮助我处理了我的 php 脚本中的类似问题,感谢您的帮助,先生。
    • 这在我需要转换产品价格以用于 Schema 的情况下帮助了我。
    • 当 Google Analytics 误解超过 1000(或 1000)的收入时很有用
    【解决方案2】:

    number_format() 带有附加参数:

    number_format(1000.5, 2, '.', '');
    

    默认为小数点分隔符的句点 (.) 和千位分隔符的逗号 (,)。我鼓励你read the documentation

    【讨论】:

      【解决方案3】:

      documentation of number_format 包含有关参数string $thousands_sep = ',' 的信息。所以这应该有效:

      number_format(1000.5, 2, '.', '');
      

      【讨论】:

        【解决方案4】:
        number_format(1000.5, 2, '.', '');
        

        http://php.net/manual/en/function.number-format.php

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-12-28
          • 2013-02-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-24
          • 1970-01-01
          相关资源
          最近更新 更多