zoukankan      html  css  js  c++  java
  • BigDecimal

    BigInteger类似,BigDecimal可以表示一个任意大小且精度完全准确的浮点数。

    案例1:微信群中发红包,金额应该用 BigDecimal 定义的数据,用 money 的钱,发count份红包,则

     1 public ArrayList<BigDecimal> send(BigDecimal totalMoney,int count){
     2         //首先准备一个集合,用来存储若干红包的金额
     3         ArrayList<BigDecimal> redList = new ArrayList<>();
     4 
     5         //首先看一下群主自己有多少钱
     6         BigDecimal leftMoney = super.getMoney();//群主当前余额
     7         if(leftMoney.compareTo(totalMoney) < 0){
     8             System.out.println("余额不足!");
     9             return redList;
    10         }
    11 
    12         //扣钱,就是重写设置余额
    13         super.setMoney(leftMoney.subtract(totalMoney));
    14 
    15         //发红包需要拆分
    16         //avg[0] 商,avg[1] 余数
    17         BigDecimal avg[] = totalMoney.divideAndRemainder(BigDecimal.valueOf(count));
    18 
    19         for (int i = 0; i < count -1; i++) {
    20             redList.add(avg[0]);
    21         }
    22         redList.add(avg[0].add(avg[1]));
    23         return redList;
    24     }
  • 相关阅读:
    第07组 Alpha冲刺(2/4)
    第07组 Alpha冲刺(1/4)
    团队项目-需求分析报告
    团队项目-选题报告
    1381 硬币游戏
    1381 硬币游戏
    1347 旋转字符串
    1344 走格子
    1305 Pairwise Sum and Divide
    1384 全排列
  • 原文地址:https://www.cnblogs.com/sethnie/p/12333607.html
Copyright © 2011-2022 走看看