【问题标题】:Show 2 decimal places without rounding off [duplicate]显示 2 位小数而不四舍五入 [重复]
【发布时间】:2012-03-28 20:56:54
【问题描述】:

可能重复:
How do I format numbers to have only two decimal places?

我已经四处寻找解决方案。假设我有一个带有这样数字的变量:

$price1 = 1597;
$price2 = 1497.85;

有没有偶数时显示小数点后2位不四舍五入的功能:

$price1 = 1597.00;
$price2 = 1497.85;

更新:

我发现了问题,SUM(total) MySQL 函数将值四舍五入:

          $result = mysql_query("SELECT SUM(total), SUM(subtotal) FROM jos_vm_order_list WHERE user_id = '$user_id'");

while($totalrow = mysql_fetch_array($result))
  {



    $total = $row['SUM(total)'];
    $total = number_format($total, 2, '.', '');
    $subtotal = $row['SUM(subtotal)'];
    $subtotal = number_format($subtotal, 2, '.', '');

  }

在使用 MySQL SUM() 函数时是否无论如何它不能四舍五入?

提前感谢您的帮助。

【问题讨论】:

  • 那么,如果她/他在这个网站上发现了与您的问题完全相同的问题,您会付钱给谁? ;)
  • 欢迎来到 Stack Overflow!提问前,请在右上角搜索以避免重复问题。
  • @TJ.: 我接受虚拟啤酒 ;)
  • 虽然对新会员很方便,但还是先google一下,肯定有帮助的。
  • “可能的重复”无效,因为 OP 询问不四舍五入,而重复只谈论四舍五入。最好在这里复制:stackoverflow.com/questions/9944001/…

标签: php mysql html


【解决方案1】:

使用number_formatPHP函数:

number_format(your_number,otional_decimals,optional_decimal_point,optional_1000_separator) 

您不能只指定小数点或 1000 分隔符,如果指定一个则需要同时指定两者。处理小数时,它的功能类似于圆 (),并将 0.5 或以上的内容放在 0.5 以上,0.5 以下。

如下:

 number_format ($price, 2);

【讨论】:

  • 您必须传递所有四个参数,否则将添加不需要的千位分隔符
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-24
  • 2012-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多