import java.math.BigDecimal;
class test0 {
public static void main(String[] args) {
String str="30.273705487000021";
System.out.println("str:" + str);
System.out.println("Double.parseDouble(str):" + Double.parseDouble(str));
BigDecimal bd = new BigDecimal("30.273705487000021");
System.out.println("bd:" + bd);
}
结果:
str:30.273705487000021
Double.parseDouble(str):30.273705487000022
bd:30.273705487000021
结论:
java string字符串转double的会存在精度损失问题,使用BigDecimal(String)创建一个具有参数所指定以字符串表示的数值的对象可以避免精度损失问题。