zoukankan      html  css  js  c++  java
  • BigDecimal精确到几位以及四舍五入



    //BigDecimal流reduce()进行累加
    BigDecimal stoNum = entry.getValue().stream()
    .map(order -> new BigDecimal(order.getStorageNum()).multiply(new BigDecimal(order.getSellPrice() == null ? "0.00" : order.getSellPrice())))
    .reduce(new BigDecimal(0), BigDecimal::add);






    BigDecimal result = null;
    //判断结果是否为零
    BigDecimal bigDecimalZero = BigDecimal.ZERO.setScale(8, BigDecimal.ROUND_HALF_UP);

    String str = String.valueOf(stoNum.toPlainString());
    int length = str.substring(str.indexOf(".") + 1, str.length()).length();
    if(length>8){
    //小数点后大于八位四舍五入
    result = stoNum.setScale(8, BigDecimal.ROUND_HALF_UP);
    }else{
    //小数点后不足8位小数补0
    result=new BigDecimal(new DecimalFormat("0.00000000#").format(stoNum));
    }
    System.out.println("值为: = " + result);
  • 相关阅读:
    lambda表达式
    切片操作
    小样本学习(Few-shot Learning)
    TensorFlow Eager 模式
    tensorflow读入数据集的方式
    综述类解读
    pyCharm永久激活
    shell脚本教程
    GNN
    Tomcat配置优化
  • 原文地址:https://www.cnblogs.com/ankang123/p/14613726.html
Copyright © 2011-2022 走看看