如果值是这样的(0.0007)以小数点后3个零结束,我得到的结果为4.0E-4 。如何在这种情况下消除指数结果
请告诉我如何解决这个问题
这是我的程序。
package com;
import java.text.DecimalFormat;
public class Test {
public static void main(String args[]) {
try {
String result = "";
Test test = new Test();
double value = 0.0004;
if (value < 1) {
result = test.numberFormat(value, 4);
} else {
result = test.numberFormat(value, 2);
}
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
public String numberFormat(double d, int decimals) {
if (2 == decimals)
return new DecimalFormat("#,###,###,##0.00").format(d);
else if (0 == decimals)
return new DecimalFormat("#,###,###,##0").format(d);
else if (3 == decimals)
return new DecimalFormat("#,###,###,##0.000").format(d);
else if (4 == decimals)
new DecimalFormat("#,###,###,##0.00##").format(d);
return String.valueOf(d);
}
}
非常感谢很多人。 – Pawan