圆整到小数点后2位 [英] Round to 2 decimal places

查看:210
本文介绍了圆整到小数点后2位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/2808535/round-a-double-to-2-significant-figures-after-decimal-point">Round小数点后双至2显著图

Possible Duplicate:
Round a double to 2 significant figures after decimal point

我有:

mkm=((((amountdrug/fluidvol)*1000)/60)*infrate)/ptwt; 

在我的Java code。在code工作正常,但返回到几位小数。我怎么把它限制在只有2或3?

in my Java code. The code works fine but returns to several decimal places. How do I limit it to just 2 or 3?

推荐答案

不要使用双打。你可能会失去一些precision。这里有一个通用的功能。

Don't use doubles. You can lose some precision. Here's a general purpose function.

public static double round(double unrounded, int precision, int roundingMode)
{
    BigDecimal bd = new BigDecimal(unrounded);
    BigDecimal rounded = bd.setScale(precision, roundingMode);
    return rounded.doubleValue();
}

您可以把它叫做

round(yourNumber, 3, BigDecimal.ROUND_HALF_UP);

precision是你想要的小数点后的数字。

"precision" being the number of decimal points you desire.

这篇关于圆整到小数点后2位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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