【问题标题】:PHP Need to impose two decimals and a commaPHP 需要强加两位小数和一个逗号
【发布时间】:2016-06-14 17:58:37
【问题描述】:

我有一些看起来像这样的 php 脚本

<?php echo "$" . round(( floatval( preg_replace( "/[^-0-9\.]/", "", $row['display_price'] ) ) * 1.03 ), 2); ?>

它的作用是这样的:$13,116.89 ea

并提出高出 3% 的价格/数字。即:$13510.4

我需要对这个值施加一个逗号和千位,并且无论返回值如何,都始终施加两位小数。我也可以将这个新计算的值呈现为一个没有问题的字符串。

感谢所有帮助。

【问题讨论】:

  • 如果您想以特定方式格式化数字,请使用number_format
  • 还是个新手,能举个例子吗?
  • 请使用文档。它有大量示例和评论,可以回答您的更多问题:php.net/manual/en/function.number-format.php

标签: php decimal comma


【解决方案1】:
$OriginalValue = 13116.89;
$IncreasedValue *= 1.03;
$FormattedNumber = "$" . number_format($IncreasedValue, 2);

这里是关于 number_format 的 PHP 文档:

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

【讨论】:

    【解决方案2】:

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

    $input = 13116.89;
    
    echo '$' . number_format($input, 2);
    
    // output: $13,116.89
    

    【讨论】:

      猜你喜欢
      • 2019-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多