public class Test {
public static void main(String[] args) {
String s = "0.0002000000";
Double aDouble = Double.parseDouble(s);
Double bDouble = Double.parseDouble(s)*10000;
System.out.println("aDouble = " + aDouble);
System.out.println("bDouble = " + bDouble);
}
}
public class Test {
public static void main(String[] args) {
String s = "0.0003000000";
Double aDouble = Double.parseDouble(s);
Double bDouble = Double.parseDouble(s)*10000;
System.out.println("aDouble = " + aDouble);
System.out.println("bDouble = " + bDouble);
}
}
public class Test {
public static void main(String[] args) {
String s = "0.0005000000";
Double aDouble = Double.parseDouble(s);
Double bDouble = Double.parseDouble(s)*10000;
System.out.println("aDouble = " + aDouble);
System.out.println("bDouble = " + bDouble);
}
}
原因分析:double乘以整数,导致精度丢失
测试使用BigDecimal的multiply
public class Test {
public static void main(String[] args) {
String s = "0.0003000000";
BigDecimal bigDecimal = new BigDecimal(s);
BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(10000));
int i = bigDecimal.multiply(bigDecimal1).intValue();
BigDecimal multiply = bigDecimal.multiply(bigDecimal1);
System.out.println("i = " + i);
System.out.println("multiply = " + multiply);
}
}
涉及金融数据等大数据加减乘除等,还是建议使用BigDecimal的API