• 浮点数转换为金额大写


     网上转账时,易发现大部分银行的网银系统都提供了类似这样的功能,即:当用户在输入转账金额数字时,下面会有个显示框用于同步显示该金额对应的大写形式,如输入:“6109.23”,则对应显示的金额大写为:“陆仟壹佰零玖元贰角叁分”。下面就贴上实现此转换功能的简单java代码,注意:该程序只支持到“万亿”级。

    view plaincopy to clipboardprint?
    ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
    /** 
     * CrazyItTest 
     * 将金额转换成人民币大写形式 
     *  
     */  
    package com.labci.java.array;  
    /** 
     * @author Bill Tu(tujiyue/iwtxokhtd) 
     * May 15, 2011[7:41:57 PM] 
     * 
     */  
    public class ConvertFloatNumToRMBFormat {  
        private String [] upperCaseArray={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};  
        private String [] moneyUnitArray={"元","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟","万"};  
        private String [] decimalArray={"角","分"};  
          
        private String []getDividedStringNum(double num){  
            long integerData=(long)num;  
            long decialData=Math.round((num-integerData)*100);  
            String decialString=String.valueOf(decialData);  
            if(decialData<10){  
                decialString="0"+decialData;  
            }  
            return new String[]{String.valueOf(integerData),decialString};  
        }  
          
        private String getIntegerStr(String integerStr){  
            String result="";  
              
            int numLength=integerStr.length();  
            if(numLength>13){  
                System.out.println("超出本程序的金额范围");  
            }else{  
                for(int i=0;i<numLength;i++){  
                    int num=integerStr.charAt(i)-48;  
                    if(num!=0){  
                        result+=upperCaseArray[num]+moneyUnitArray[numLength-i-1];  
                    }else{  
                        result+=upperCaseArray[num];  
                    }  
                }  
            }  
              
            return result;  
        }  
          
        private String getDecimalStr(String decimalStr){  
            String result="";  
            if(null!=decimalStr&& !"".equals(decimalStr)){  
                int allNum=Integer.parseInt(decimalStr);  
                if(allNum>0){  
                    int numLength=decimalStr.length();  
                      
                    for(int i=0;i<numLength;i++){  
                        int num=decimalStr.charAt(i)-48;  
                        if(num!=0){  
                            result+=upperCaseArray[num]+decimalArray[i];  
                        }else{  
                            if(i<1){  
                                result+=upperCaseArray[num];  
                            }  
                        }  
                    }  
                }  
            }  
            return result;  
        }  
          
        private String getConvertedString(double moneyData){  
            String []getDividedStringNumArray=getDividedStringNum(moneyData);  
            String integerStr=getDividedStringNumArray[0];  
            String result=getIntegerStr(integerStr);  
            if(null!=result && !"".equals(result)){  
                if(getDividedStringNumArray.length>1){  
                    String decimalStr=getDividedStringNumArray[1];  
                    result+=getDecimalStr(decimalStr);  
                }  
            }  
            return result;  
        }  
          
        /** 
         * @param args 
         */  
        public static void main(String[] args) {  
            ConvertFloatNumToRMBFormat nr=new ConvertFloatNumToRMBFormat();  
            double moneyData=132210216109.11;  
            System.out.println(nr.getConvertedString(moneyData));  
        }  
    }  
    
    运行结果:

    壹仟叁佰贰拾贰亿壹仟零贰拾壹万陆仟壹佰零玖元壹角壹分

  • 相关阅读:
    linux学习之路第八天(linux文件权限详解)
    linux学习之路第八天(组管理和权限管理)
    python 多线程示例
    python scapy 网卡发包
    python scapy 网卡抓包
    python 返回数组的索引
    MPLS 网络中的 MTU
    mysql 导入导出sql文件
    linux 修改MTU值
    ovs 源mac, 目的src 互换
  • 原文地址:https://www.cnblogs.com/xiaofengfeng/p/2097827.html
Copyright © 2020-2023  润新知