ListString energyList = new ArrayList; 把energyList 的string值转化成Bigdecimal类型并计算所有值累加值累加值保留两位小数
List
BigDecimal sum = BigDecimal.ZERO; for (String energy : energyList) { BigDecimal energyValue = new BigDecimal(energy); sum = sum.add(energyValue); }
sum = sum.setScale(2, RoundingMode.HALF_UP); System.out.println(sum); // Prints the sum with two decimal places
原文地址: http://www.cveoy.top/t/topic/h07u 著作权归作者所有。请勿转载和采集!