所以我有一个等于1234的双重设置,我想将小数位移到12.34
所以要做到这一点,我将.1乘以1234两次,有点像这样
double x = 1234;
for(int i=1;i<=2;i++)
{
x = x*.1;
}
System.out.println(x);
Run Code Online (Sandbox Code Playgroud)
这将打印结果"12.340000000000002"
有没有一种方法,而不是简单地将其格式化为两位小数,以正确的双重存储12.34?
我需要在Java中进行一些浮点运算,如下面的代码所示:
public class TestMain {
private static Map<Integer, Double> ccc = new HashMap<Integer, Double>() {
{ put(1, 0.01); put(2, 0.02); put(3, 0.05); put(4, 0.1); put(6, 0.2);
put(10, 0.5); put(20, 1.0); put(30, 2.0); put(50, 5.0); put(100, 10.0);
}
};
Double increment(Double i, boolean up) {
Double inc = null;
while (inc == null) {
inc = ccc.get(i.intValue());
if (up)
--i;
else
++i;
}
return inc;
}
public static void main(String[] args) {
TestMain tt = new TestMain();
for (double i = …Run Code Online (Sandbox Code Playgroud) 我在Java中执行计算时遇到了一些困难.这是我想要做的 -
((0.053800 * (500000/1000)) + 4) * 0.85
在我的java应用程序中,它返回26.264999999999997,如果你向上舍入到2位小数,则变为26.26.
但在MS Excel中,相同的公式返回26.265000 ..因此舍入结果为26.27.
如果我的Java应用程序返回的值不正确,我该怎么做才能纠正它?