日期:2014-05-20  浏览次数:20758 次

如何将double中的那些0去掉?
double   d1=123.12;
d1-=100.00;
System.out.println(d1);

运行结果是
23.120000000000005

如何只要23.12?

再例如如果结果是432.1234000000,只要432.1234

------解决方案--------------------
用大数类
------解决方案--------------------
看看下面两个类的文档
java.text.NumberFormat;
java.text.DecimalFormat;
------解决方案--------------------
Here is an example:

double d1=123.12;
d1-=100.00;
DecimalFormat nf = new DecimalFormat( "0.00 ");
System.out.println(nf.format(d1));

nf = new DecimalFormat( "0.0000 ");
System.out.println(nf.format(432.1234000000));

The better way is:

BigDecimal b= new BigDecimal(123.12D);
b.add(new BigDecimal(-100.00));
System.out.println(b.doubleValue());



------解决方案--------------------
CSDN的文档里有这个东东,我已经使用了,感觉很爽.
------解决方案--------------------
学习。 我只会转成字符串后截取。。
------解决方案--------------------
用代码来转化精度
我机子里面的代码没了
下回找到了 发给你`````
------解决方案--------------------
显示?
API去看看 printf()
------解决方案--------------------
public class Multi {
public static void main(String args[]) {
float i;
float k;
float sum;
i = Float.parseFloat( "2.00 ");
k = Float.parseFloat( "1.10 ");
sum = i - k;
System.out.println( "sum "+sum);
}

}