本文介绍了圆整到小数点后2位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复: 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位
发布评论