浮点数转换成人民币

public class NumToRmb {


    private final String[] hanArr = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private final String[] unitArr = {"仟", "", "十", "佰"};
    private final String[] tag = {"元", "万", "亿"};
    
    public String toHanStr(double num) {
        
        String result = "";
        
        long zheng = (long)num;
        //long xiao = Math.round((num - zheng) * 100);//如果需要四舍五入,就用这个方法,但是小数点后
                                                      //两位依次为99时,第三位不能>=5,否则会报错
        long xiao = (long)((num - zheng) * 100);//如果不需要四舍五入,就用这个方法
        
        /*整数部分为0单独考虑*/
        if(0 == zheng) {
            
            int tempJiao = (int)(xiao / 10);
            int tempFen = (int)(xiao % 10);
            
            if(tempJiao != 0) {
                result += hanArr[tempJiao] + "角";
            }
            if(tempFen != 0) {
                result += hanArr[tempFen] + "分";
            }
            
            return result;
        }
        
        String zhengStr = String.valueOf(zheng);
        int len = zhengStr.length();
        
        int tempLen = 0;
        
        /*处理整数部分*/
        for(int i = 0; i < len; i++) {
            
            int temp = zhengStr.charAt(i) - 48;
            int part = (len - i - 1) / 4; //当前字符处于哪个段
            int location = (len - i - 1) % 4; //当前字符处于该段的具体哪个位置
            
            if(location != 0) { //不是该段的最后一个
                
                if(temp != 0) { //当前字符不为0
                    result += hanArr[temp] + unitArr[(len - i) % 4]; continue;
                } else { //当前字符为0
                    tempLen = result.length();
                    if(3 == location && result.charAt(tempLen - 1) != '零') { //当前字符为0且为该段第一个
                        result += "零"; continue;
                    } else {
                        tempLen = result.length();
                        if(result.charAt(tempLen - 1) == '零') {
                            continue;
                        } else {
                            result += "零"; continue;
                        }
                    }
                }
            } else { //是该段最后一个
                if(temp != 0) {
                    result += hanArr[temp] + tag[part]; continue;
                } else {
                    tempLen = result.length();
                    if(result.charAt(tempLen - 1) != '零') {
                        result += tag[part]; continue;
                    } else {
                        if(result.charAt(tempLen - 2) == '亿' || result.charAt(tempLen - 2) == '万') {
                            continue;
                        } else {
                            result = result.substring(0, tempLen - 1) + tag[part]; continue;
                        }
                    }
                }
            }
            
        }
        
        /*处理小数部分*/
        int jiao = (int)(xiao / 10);
        int fen = (int)(xiao % 10);
        
        if(jiao != 0) {
            result += hanArr[jiao] + "角";
        }
        if(fen != 0) {
            result += hanArr[fen] + "分";
        }
        
        /*排除输入形式如60 0000 0000.00,输出为“陆十亿零”的情况*/
        tempLen = result.length();
        if(result.charAt(tempLen - 1) == '零') {
            result = result.substring(0, tempLen - 1);
        }
        tempLen = result.length();
        if(result.charAt(tempLen - 1) == '元') {
            result += "整";
        }
        if(result.charAt(tempLen - 1) == '万' || result.charAt(tempLen - 1) == '亿') {
            result += "元整";
        }
        
        return result;
    }
    public static void main(String[] args) {
        
        NumToRmb ntr = new NumToRmb();
        System.out.println(ntr.toHanStr(6000000000.0000));
        System.out.println(ntr.toHanStr(600100.000));
        System.out.println(ntr.toHanStr(1006.333));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值