BigDecimal multiply乘法运算
BigDecimal multiply乘法运算
package com.example.core.mydemo; import java.math.BigDecimal; public class MulTest { public static void main(String[] args) { BigDecimal distancedbg = new BigDecimal(0.0055); //输出:5 System.out.println(distancedbg.multiply(new BigDecimal(1000)).longValue()); //输出:6 System.out.println(new BigDecimal(distancedbg.multiply(new BigDecimal(1000)).longValue()).multiply(new BigDecimal(1.4)).longValue()); //为什么输出是6,拆分结果: //5 System.out.println(new BigDecimal(distancedbg.multiply(new BigDecimal(1000)).longValue())); //6.999999999999999555910790149937383830547332763671875 System.out.println(new BigDecimal(distancedbg.multiply(new BigDecimal(1000)).longValue()).multiply(new BigDecimal(1.4))); //6 System.out.println(new BigDecimal(distancedbg.multiply(new BigDecimal(1000)).longValue()).multiply(new BigDecimal(1.4)).longValue()); //输出:7.699999999999999553135232588374492479488253593444824218750000 System.out.println(distancedbg.multiply(new BigDecimal(1000)).multiply(new BigDecimal(1.4))); BigDecimal big = distancedbg.multiply(new BigDecimal(1000)).multiply(new BigDecimal(1.4)); long result = big.setScale(0, BigDecimal.ROUND_HALF_UP).longValue(); //输出:8 (四舍五入,不保留小数点) System.out.println(result); double result2 = big.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue(); ////输出:7.7 (四舍五入,保留1位小数点) System.out.println(result2); long result3 = big.longValue(); ////输出:7 (直接强转取整) System.out.println(result3); } }