显示2个小数位而不四舍五入 [英] Show 2 decimal places without rounding off

查看:209
本文介绍了显示2个小数位而不四舍五入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何将数字格式化为只有两位小数地方?

Possible Duplicate:
How do I format numbers to have only two decimal places?

我到处都在寻找解决方案.假设我有一个变量,其数字是这样的:

I have looked all over for a solution. Say i have a variable with a number like this:

$price1 = 1597;
$price2 = 1497.85;

是否有一个函数显示偶数位的小数点后2位:

Is there a function that displays 2 decimal places without rounding off when there is a even number:

$price1 = 1597.00;
$price2 = 1497.85;

更新:

我发现了问题,MySQL SUM(总计)函数将值取整:

          $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()函数时,是否仍然无法舍入?

谢谢您的帮助.

推荐答案

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

number_format($number, 2, '.', '');

这篇关于显示2个小数位而不四舍五入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆