gpt4 book ai didi

java - 如何舍入 Double 数据类型值

转载 作者:行者123 更新时间:2023-12-02 01:28:33 24 4
gpt4 key购买 nike

以下是我的代码

          String Send_Amount = SendAmount.getText().toString();
Double lastSendAmount = Double.parseDouble(Send_Amount);
Double LastRate = Double.parseDouble(RATE_FOR_ONE_UNIT);
Double LastReceiveAmount = lastSendAmount * LastRate;
ReceiveAmount.setText(LastReceiveAmount.toString());

如何将 LastReceiveAmount 舍入为 "x.ab""x.ab+0.01"(当 b>5 时)

最佳答案

显然,没有一种“好的”方法可以做到这一点。但有很多解决方法。

十进制格式:

DecimalFormat df = new DecimalFormat("#.00");

字符串格式:

String.format("%.2f", d);

数学解决方法:

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

希望这有帮助。祝你好运。

关于java - 如何舍入 Double 数据类型值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56483506/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com