java 给定价格和付款金额计算找零金额-挖财笔试编程题

    /**
     * 根据应收金额 dcost, 和dCash实收现金计算找零多少
     * @param dCash
     *            现金
     * @param dCost
     *            金额
     */
    public static void printChange( double dCash , double dCost ) {
        if (dCash < 0 || dCost < 0 || dCash < dCost) {
            return;
        }
        DecimalFormat format = new DecimalFormat("0.00");
        // System.out.println(format.format(dCash));
        // System.out.println(format.format(dCost));//用于对double类型数据的数据小数点后几位指定
        /*
         * 对于金额使用BigDecimal处理,可以截尾
         * 在需要精确答案的地方,要避免使用float和double;对于货币计算,使用int,long或BigDecimal会更好.
         */
        BigDecimal bigDecimal = new BigDecimal(format.format(dCash));

        BigDecimal decimal = bigDecimal
.subtract(new BigDecimal(format
                .format(dCost)));//需要找的钱
        // System.out.println(decimal.doubleValue());
        double charges[] = { 100, 50, 20, 10, 5, 2, 1, 0.5, 0.2, 0.1, 0.05,
                0.02, 0.01 };

        double money = decimal.doubleValue();// 获取找零的金额,
        // System.out.println(money);
        int chargesNum[] = new int[charges.length];
        for (int i = 0; i < charges.length; i++)
            chargesNum[i] = 0;// 记录每一个面额的纸币有多少长
        int i = 0;
        while (money > 0) {

            while (charges[i] > money)
                i++;
            money -= charges[i];
            money = Double.valueOf(format.format(money)).doubleValue();// 对减去一个浮点型数字后进行小数点格式化,格式化带两个小数点的实数。
            System.out.println(money);
            chargesNum[i] += 1;

        }
        // 把找零的数字输出
        System.out.print("找零金额:" + decimal.doubleValue() + " :");
        for (int j = 0; j < chargesNum.length; j++) {
            if (chargesNum[j] > 0) {
                if (j < 7) {
                    System.out.print(chargesNum[j] + "张" + charges[j] + "元  ");
                } else if (j < 10) {
                    System.out.print(chargesNum[j] + "张" + charges[j] + "角  ");
                } else {
                    System.out.print(chargesNum[j] + "张" + charges[j] + "分  ");
                }

            }
        }
    }

测试:printChange(5, 1.11);

输出结果:

1.89
0.89
0.39
0.19
0.09
0.04
0.02
0.0
找零金额:3.89 :1张2.0元  1张1.0元  1张0.5角  1张0.2角  1张0.1角  1张0.05分  2张0.02分   

关于使用BigDecimal类来进行计算的时候,主要分为以下步骤:

          1、用float或者double变量构建BigDecimal对象。

         2、通过调用BigDecimal的加,减,乘,除等相应的方法进行算术运算。

         3、把BigDecimal对象通过相应xxxValue()方法的转换成float,double,int等类型。

      一般来说,创建BigDecimal实例可以使用BigDecimal的构造方法或者静态方法的valueOf()方法把基本类型的变量构建成BigDecimal对象。

对于加减乘除算术运算提供了工具方法

public BigDecimal add(BigDecimal value);         //加法
2 public BigDecimal subtract(BigDecimal value);  //减法                  
3 public BigDecimal multiply(BigDecimal value);    //乘法               
4 public BigDecimal divide(BigDecimal value);      //除法               
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值