以下是我的代码
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/