在线等任意输入一个五位数,输出这个字符串的中文读法
例如:
  输入 10030
  输出 一万零三十要求对输入的数据串进行检查,注意各种异常和边界的检测.

解决方案 »

  1.   

    /**
     * 将金额数字字符串按指定的货币类型,转换成中文大写金额字符串。
     * @param currency 金额数字字符串
     * @param type 货币类型
     * @return
     */
    public static String num2Chn(String currency,String type) {
    String chnCurrency = "", tmpCurrency = currency,errMsg = "";
    int i;
    for (i = currency.length() - 1; i >= 0; i--) {
    tmpCurrency = tmpCurrency.replaceAll(",", "");//替换tomoney()中的“,”
    tmpCurrency = tmpCurrency.replaceAll(" ", "");//替换tomoney()中的空格
    }
    tmpCurrency = tmpCurrency.replaceAll("¥", "");//替换掉可能出现的¥字符
    double d = Double.parseDouble(tmpCurrency);
    if (Double.isNaN(d)) { //验证输入的字符是否为数字
    errMsg = "error:请检查小写金额是否正确";
    return errMsg;
    }
    //---字符处理完毕,开始转换,转换采用前后两部分分别转换---//
    String[] part = tmpCurrency.split("[.]");
    String newchar = "";
    //小数点前进行转化
    int frontPart = part[0].length();
    for (i = frontPart - 1; i >= 0; i--) {
    if (part[0].length() > 12) {
    return errMsg = "error:位数过大,无法计算";
    }
    //若数量超过拾亿单位,提示
    String tmpnewchar = "";
    String perchar = part[0].substring(i, i+1);
    if (perchar.equals("0")) {
    tmpnewchar = "零" + tmpnewchar;
    } else if (perchar.equals("1")) {
    tmpnewchar = "壹" + tmpnewchar;
    } else if (perchar.equals("2")) {
    tmpnewchar = "贰" + tmpnewchar;
    } else if (perchar.equals("3")) {
    tmpnewchar = "叁" + tmpnewchar;
    } else if (perchar.equals("4")) {
    tmpnewchar = "肆" + tmpnewchar;
    } else if (perchar.equals("5")) {
    tmpnewchar = "伍" + tmpnewchar;
    } else if (perchar.equals("6")) {
    tmpnewchar = "陆" + tmpnewchar;
    } else if (perchar.equals("7")) {
    tmpnewchar = "柒" + tmpnewchar;
    } else if (perchar.equals("8")) {
    tmpnewchar = "捌" + tmpnewchar;
    } else if (perchar.equals("9")) {
    tmpnewchar = "玖" + tmpnewchar;
    }
    if (part[0].length() - i - 1 == 0) {
    if(type.equals("RMB")) {tmpnewchar = tmpnewchar + "元";}
    else if(type.equals("USD")) {tmpnewchar = tmpnewchar + "美元";}
    } else if (part[0].length() - i - 1 == 1) {
    if (!perchar.equals("0"))
    tmpnewchar = tmpnewchar + "拾";
    } else if (part[0].length() - i - 1 == 2) {
    if (!perchar.equals("0"))
    tmpnewchar = tmpnewchar + "佰";
    } else if (part[0].length() - i - 1 == 3) {
    if (!perchar.equals("0"))
    tmpnewchar = tmpnewchar + "仟";
    } else if (part[0].length() - i - 1 == 4) {

    tmpnewchar = tmpnewchar + "万";
    } else if (part[0].length() - i - 1 == 5) {
    if (!perchar.equals("0"))
    tmpnewchar = tmpnewchar + "拾";
    } else if (part[0].length() - i - 1 == 6) {
    if (!perchar.equals("0"))
    tmpnewchar = tmpnewchar + "佰";
    } else if (part[0].length() - i - 1 == 7) {
    if (!perchar.equals("0"))
    tmpnewchar = tmpnewchar + "仟";
    } else if (part[0].length() - i - 1 == 8) {

    tmpnewchar = tmpnewchar + "亿";
    } else if (part[0].length() - i - 1 == 9) {
    if (!perchar.equals("0"))
    tmpnewchar = tmpnewchar + "拾";
    } else if (part[0].length() - i - 1 == 10) {
    if (!perchar.equals("0"))
    tmpnewchar = tmpnewchar + "佰";
    } else if (part[0].length() - i - 1 == 11) {
    if (!perchar.equals("0"))
    tmpnewchar = tmpnewchar + "仟";
    }
    newchar = tmpnewchar + newchar;
    }
    //小数点之后进行转化
    if (currency.indexOf(".") != -1) {
    if (part[1].length() > 2) {
    errMsg = "小数点之后只能保留两位,系统将自动截段";
    part[1] = part[1].substring(0, 2);
    }
    for (i = 0; i < part[1].length(); i++) {
    String tmpnewchar = "";
    String perchar = part[1].substring(i,i+1);
    if(perchar.equals("0")) {
    tmpnewchar = "零" + tmpnewchar;
    }else if(perchar.equals("1")){
    tmpnewchar = "壹" + tmpnewchar;
    }else if(perchar.equals("2")){
    tmpnewchar = "贰" + tmpnewchar;
    }else if(perchar.equals("3")){
    tmpnewchar = "叁" + tmpnewchar;
    }else if(perchar.equals("4")){
    tmpnewchar = "肆" + tmpnewchar;
    }else if(perchar.equals("5")){
    tmpnewchar = "伍" + tmpnewchar;
    }else if(perchar.equals("6")){
    tmpnewchar = "陆" + tmpnewchar;
    }else if(perchar.equals("7")){
    tmpnewchar = "柒" + tmpnewchar;
    }else if(perchar.equals("8")){
    tmpnewchar = "捌" + tmpnewchar;
    }else if(perchar.equals("9")){
    tmpnewchar = "玖" + tmpnewchar;
    }
    if (i == 0)
    tmpnewchar = tmpnewchar + "角";
    if (i == 1)
    tmpnewchar = tmpnewchar + "分";
    newchar = newchar + tmpnewchar;
    }
    }
    //替换所有无用汉字
    while (newchar.indexOf("零零") != -1)
    newchar = newchar.replaceAll("零零", "零");
    newchar = newchar.replaceAll("零亿", "亿");
    newchar = newchar.replaceAll("零万", "万");
    newchar = newchar.replaceAll("亿万", "亿");
    if(type.equals("RMB")){
    newchar = newchar.replaceAll("零元", "元");
    }else if(type.equals("USD")){
    newchar = newchar.replaceAll("零美元", "美元");
    }
    newchar = newchar.replaceAll("零角", "");
    newchar = newchar.replaceAll("零分", "");
    if(type.equals("USD")){
    if (newchar.substring(newchar.length() - 2,newchar.length()) == "美元"
    || newchar.substring(newchar.length() - 1,newchar.length()) == "角")
    newchar = newchar + "整";
    }else if(type.equals("RMB")){
    if (newchar.substring(newchar.length() - 1,newchar.length()) == "元"
    || newchar.substring(newchar.length() - 1,newchar.length()) == "角")
    newchar = newchar + "整";
    }
    return newchar;
    }
      

  2.   

    鄙人的程序,
    1、能将一个long、double、或 String 转换为 中文的大写或小写,
    2、能将应该 double 转换为 RMB(大写)/*
     * NumberToZH.java
     *
     * Created on 2006年6月30日, 下午5:35
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */package org.trumplet.toZH;import java.text.DecimalFormat;/**
     *
     * @author Trumplet
     */
    public class NumberToZH {
        static final String zhnum_0 = "零壹贰叁肆伍陆柒捌玖";
        static final String zhnum = "零一二三四五六七八九";
        static final String[] zhnum1 = {"","十","百","千"};
        static final String[] zhnum1_0 = {"","拾","佰","仟"};
        static final String[] zhnum2 = {"","万","亿","万亿","亿亿"};
        /** Creates a new instance of NumberToZH */
        public NumberToZH() {
        }
        private static String numberToZH4(String s,boolean fan){
            StringBuffer sb = new StringBuffer();
            if (s.length()!=4) return null;
            for (int i=0;i<4;i++){
                char c1 = s.charAt(i);
                if (c1=='0' && i>1 && s.charAt(i-1)=='0')  continue;
                if (c1!='0' && i>1 && s.charAt(i-1)=='0') sb.append('零');
                if (c1!='0') {
                    if (fan) {
                        sb.append(zhnum_0.charAt(c1-48));
                        sb.append(zhnum1_0[4-i-1]);
                    } else {
                        sb.append(zhnum.charAt(c1-48));
                        sb.append(zhnum1[4-i-1]);
                    }
                }
            }
            return new String(sb);
        }
        public static String numberToZH(long n,boolean fan){
            StringBuffer sb = new StringBuffer();
            String strN = "000"+n;
            int strN_L = strN.length()/4;
            strN = strN.substring(strN.length()-strN_L*4);
            for (int i=0;i<strN_L;i++){
                String s1 = strN.substring(i*4,i*4+4);
                String s2 = numberToZH4(s1,fan);
                sb.append(s2);
                if(s2.length()!=0) sb.append(zhnum2[strN_L-i-1]);
            }
            String s = new String(sb);
            if (s.length()!=0 && s.startsWith("零"))
                s = s.substring(1);
            return s;
        }
        
        public static String numberToZH(double d,boolean fan){
            return numberToZH("" + d,fan);
        }
        public static String numberToZH(String str,boolean fan){
            StringBuffer sb = new StringBuffer();
            int dot = str.indexOf(".");
            if (dot<0) dot=str.length();
            
            String zhengshu = str.substring(0,dot);
            sb.append(numberToZH(Long.parseLong(zhengshu),fan));
            if (dot!=str.length()){
                sb.append("点");
                String xiaoshu = str.substring(dot+1);
                for (int i=0;i<xiaoshu.length();i++){
                    if(fan){
                        sb.append(zhnum_0.charAt(Integer.parseInt(xiaoshu.substring(i,i+1))));
                    } else {
                        sb.append(zhnum.charAt(Integer.parseInt(xiaoshu.substring(i,i+1))));
                    }
                }
            }
            String s = new String(sb);
            if (s.startsWith("零")) s = s.substring(1);
            if (s.startsWith("一十")) s = s.substring(1);
            while (s.endsWith("零")){s = s.substring(0,s.length()-1);}
            if (s.endsWith("点")) s = s.substring(0,s.length()-1);
            return s;
        }
        
        public static String numberToRMB(double rmb){
            String strRMB = "" + rmb;
            DecimalFormat nf = new DecimalFormat("#.#");
            nf.setMaximumFractionDigits(2);
            strRMB=nf.format(rmb).toString();
            //System.out.println(strRMB);
            strRMB=numberToZH(strRMB,true);
            if (strRMB.indexOf("点")>=0){
                strRMB = strRMB + "零";
                strRMB = strRMB.replaceAll("点","圆");
                String s1 = strRMB.substring(0,strRMB.indexOf("圆")+1);
                String s2 = strRMB.substring(strRMB.indexOf("圆")+1);
                strRMB=s1 + s2.charAt(0) + "角" + s2.charAt(1) + "分整";
            }else {
                strRMB=strRMB+"圆整";
            }
            return "人民币(大写):" + strRMB;
        }
        
        public static void main(String[] args) {
            System.out.println(numberToRMB(12345.67));
            System.out.println(numberToZH(12345,false));
            System.out.println(numberToZH(12345.67,false));
            System.out.println(numberToZH("12345.67",false));
            System.out.println(numberToZH(12345,true));
            System.out.println(numberToZH(12345.67,true));
            System.out.println(numberToZH("12345.67",true));
        }
    }
      

  3.   


    public class Test {
    public static String translate(String str){
      char[] cs = new char[str.length()];
      boolean flag2 = true;
      boolean flag = true;
      for(int i=0;i<cs.length;i++){
      cs[i] = str.charAt(i);
      //System.out.println(cs[i]);
      }
      str="";
      for(int i=cs.length-1,j=1;i>=0;i--,j++){
      int m = j%4;
      String ss = sToL(cs[i]);
      if(m==1) {    
       if(!ss.equals(""))
       {
        str = ss + str;
        flag2 = true;
       }
       else flag2 = false;
      }
      if(m==2) {
      if(ss.equals("")){
      if(flag2){
      str = "零" + str;
      flag2 = false;
      }
      }else
      {
       str = ss + "拾" + str;
       flag2 = true;
      }
      }
      if(m==3) {
      if(ss.equals("")){
      if(flag2){
      str = "零" + str;
      flag2 = false;
      }
      }else{
      str = ss + "佰" + str;
      flag2 = true;
      }
      
      }
      if(m==0) {
      if(ss.equals("")){
      if(flag2){
      str = "零" + str;
      flag2 = false;
      }
      }else{
      str = ss + "仟" + str;
      flag2 = true;
      }
      if(i>0) {
      if(flag)
      str = "萬"  + str;
      
      else 
      str = "億" + str;
      flag = !flag;
      }
      }
      }
      return escape(str);
      }
      
      public static String escape(String str){   
      int pos = str.indexOf("億");
      while(pos != -1){
      if(str.charAt(pos + 1)=='萬'){
      str = str.substring(0,pos+1) + str.substring(pos +2);   
      }
      pos = str.indexOf("億",pos+1);
      }
      return str;
      }
      
      public static String sToL(char c){
      if(c=='0'){
      return "";
      }
      if(c=='1'){
      return "壹" ;
      }
      if(c=='2'){
      return "貳" ;
      }
      if(c=='3'){
      return "叁" ;
      }
      if(c=='4'){
      return "肆" ;
      }
      if(c=='5'){
      return "伍" ;
      }
      if(c=='6'){
      return "陸" ;
      }
      if(c=='7'){
      return "柒" ;
      }
      if(c=='8'){
      return "捌" ;
      }   
      return "玖" ;
      }
      public static void main(String[] args){
      System.out.println(translate("1234567890987654321"));
      }
    }