java舍入2位_关于java:舍入到小数点后2位

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:

Round a double to 2 significant figures after decimal point

我有:

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

在我的Java代码中。代码工作正常,但返回到几个小数位。如何将其限制为2或3?

不一样。链接的dupe候选者讨论了如何打印整数,而不是如何对存储在变量中的值进行实际舍入。

不要用双打。你可能会失去一些精度。这是一个通用函数。

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);

"精度"是您想要的小数点数量。

thanks for the time to bluedevil以回复。我想得到你的建议和bigdecimal cannot be说是错误的时间分辨的型。我害怕我need the New to this和idiots指南!

java.math.bigdecimal进出口

灿烂,谢谢:))

只需使用math.round()。

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

mkm= (double)(Math.round(mkm*100))/100;

我在这里为你math.round工作要为地方arent As 2小数问题

double formattedNumber = Double.parseDouble(new DecimalFormat("#.##").format(unformattedNumber));

为我工作:)

BigDecimal a = new BigDecimal("12345.0789");

a = a.divide(new BigDecimal("1"), 2, BigDecimal.ROUND_HALF_UP);

//Also check other rounding modes

System.out.println("a >>"+a.toPlainString()); //Returns 12345.08

乘以1000,四舍五入,再除以1000。

对于基本的Java:HTTP://ListBoo.Oracle .COM/JavaSe/TutoRale/GETSARTID/NETX.HTML和HTTP://DeLoad .Oracle .COM/JavaSe/TutoRale/Java/Nojx.HTML

thorbjorn thanks for the回复。在过去的distant when used to the Basic程序会在你使用的方式(尽管它suggested not int(x)轮)。我在给人物how to this out虽然Android的Java。

尝试:

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

int newNum = (int) mkm;

mkm = newNum/1000f; // Will return 3 decimal places

travega HI,感谢你帮助我以the time with this。我和你supplied队列糊copied the,but the syntax error—AM获取误差,localvariabledeclarationstatement to insert";"是完整的。三是要checked the above the队列;在我们的地方。

创建一个名为Round的类,并尝试在代码中使用Round as Round.Round方法(TargetValue,RoundToDecimalPlaces)

public class Round {

public static float round(float targetValue, int roundToDecimalPlaces ){

int valueInTwoDecimalPlaces = (int) (targetValue * Math.pow(10, roundToDecimalPlaces));

return (float) (valueInTwoDecimalPlaces / Math.pow(10, roundToDecimalPlaces));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值