圆整到小数点后2位

编程入门 行业动态 更新时间:2024-10-22 16:33:42
本文介绍了圆整到小数点后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位

本文发布于:2023-07-06 07:58:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047595.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小数点

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!