天道酬勤,学无止境

mapToDouble

Lambda表达式mapToDouble.sum精度问题

public static void main(String[] args) { List<Double> doubles = Arrays.asList(new Double(3.3), new Double(3.3), new Double(3.3)); double sum = doubles.stream().mapToDouble(Double::doubleValue).sum(); System.out.println(sum);// 9.899999999999999 System.out.println(formatDigit(sum,2));// 9.9 System.out.println(formatDigit_down(sum, 2)); // 9.89 List<Double> doubles1 = Arrays.asList(new Double(1.1), new Double(1.1), new Double(1.1)); double sum1 = doubles1.stream().mapToDouble(Double::doubleValue).sum(); System.out.println(sum1);// 3.3000000000000003 System.out.println(formatDigit(sum1,2));// 3.3

2021-05-12 13:11:29    分类:博客    sum   mapToDouble   java