金融程序必用代码块之一

金融程序必用代码块之一 ---------------------- 将123转化为 一百二十三  

  定义一个接口 IMoney

    

package com.zhou.Money;

public interface IMoney {

    public  String moneyToUpper(String paranMoney);

}

     实现类   

MoneyUpper 

   

package com.zhou.Money;

import java.math.BigDecimal;

/**
 * @author zyw
 * @Email zyw205@gamil.com
 */
public class MoneyUpper implements IMoney {

    public String moneyToUpper(String paranMoney) {
        System.out.println("输入小写的金额:" + paranMoney);
        StringBuffer strMoney = new StringBuffer();
        String[] numList = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
        String[] unitList = { "分", "角", "元", "拾", "佰", "仟", "万","十万","百万","千万","亿"};
        BigDecimal money = new BigDecimal(paranMoney);
        double douMoney = money.doubleValue() * 100;
        if (money.intValue() == 0) {
            return "零元整";
        }
        // 只取小数后两位
        String strNum = money.setScale(2, BigDecimal.ROUND_DOWN).toString();
        strNum = strNum.substring(0, strNum.length() - 3)
                + strNum.substring(strNum.length() - 2, strNum.length());
        int len = strNum.length();
        int zero = 0;
        for (int i = 0; i < len; i++) {
            String str = strNum.substring(i, i + 1).trim();
            int nuitNum = len - i - 1;
            int num = Integer.valueOf(str).intValue();
            if (num == 0) {
                zero++;
                if (nuitNum == 2 || nuitNum == 6 || nuitNum == 10) {
                    if (nuitNum == 2 || zero < 4) 
                        strMoney.append(unitList[nuitNum]);
                        zero = 0;
                }
                } else {
                    if (zero > 0) {
                        strMoney.append(numList[0]);
                        zero = 0;
                    }
                    strMoney.append(numList[num]);
                    strMoney.append(unitList[nuitNum]);
                }
            }
            if (zero > 0) 
                strMoney.append("整");
                System.out.println("输入大写金额:" + strMoney.toString());
                return strMoney.toString();
        
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值