double转BigDecimal,数变大
程序员文章站
2022-07-14 11:16:28
...
源:http://blog.csdn.net/linbrain0000/article/details/49716997
评:
环境Windows 7 64位,JDK1.8.0_40
[java] view plain copy
public static void main(String[] args) {
double d = 1.1;
BigDecimal bd1 = new BigDecimal(d); //(1)
BigDecimal bd2 = BigDecimal.valueOf(d);//(2)
BigDecimal bd3 = new BigDecimal("1.1");//(3)
System.out.println(bd1);//打印1.100000000000000088817841970012523233890533447265625
System.out.println(bd2);//打印1.1
System.out.println(bd3);//打印1.1
System.out.println(bd1.compareTo(bd2));//打印1
System.out.println(bd1.compareTo(bd3));//打印1
System.out.println(bd2.compareTo(bd3));//打印0
}
用(1)方法传double直接new BigDecimal,数会变大,(2)(3)不会,建议不要用(1)
评:
环境Windows 7 64位,JDK1.8.0_40
[java] view plain copy
public static void main(String[] args) {
double d = 1.1;
BigDecimal bd1 = new BigDecimal(d); //(1)
BigDecimal bd2 = BigDecimal.valueOf(d);//(2)
BigDecimal bd3 = new BigDecimal("1.1");//(3)
System.out.println(bd1);//打印1.100000000000000088817841970012523233890533447265625
System.out.println(bd2);//打印1.1
System.out.println(bd3);//打印1.1
System.out.println(bd1.compareTo(bd2));//打印1
System.out.println(bd1.compareTo(bd3));//打印1
System.out.println(bd2.compareTo(bd3));//打印0
}
用(1)方法传double直接new BigDecimal,数会变大,(2)(3)不会,建议不要用(1)
上一篇: extends和super的区别