JAVA 将数字字符串转换成中文形式

public class MainClass3 {

    /**
    * @param args
    * @deprecated 完成小数的中文转换
    * @deprecated 核心工式:unit[(len - i - 1) % 8]
    * @deprecated
    * 1-0-1=0%8 亿
    * 2-0-1=1%8 拾
    * 3-0-1=2%8 佰
    * 4-0-1=3%8 仟
    * 5-0-1=4%8 万
    * 6-0-1=5%8 拾
    * 7-0-1=6%8 佰
    * 8-0-1=7%8 仟
    * 9-0-1=9%8 亿
    */
    public static void main(String[] args) {

    String temp0 = "987.65";// "009081";
    String temp = temp0;

    if (temp0.startsWith("0")) {
       temp = String.valueOf(Integer.parseInt(temp0));
       // System.out.println(Integer.parseInt(temp));
    }
    // int itemp = Integer.valueOf(temp);
    // System.out.println("String --> Integer"+itemp);
    StringBuffer retStrBuf = new StringBuffer();// 用来拼接数字串
    int dot = 0; // 小数点位置
    int ivalue = 0;// 保存每一个位上的数
    int len = 0;// 保存数字的长度
    for (int i = 0; i < temp.length(); i++) {
       System.out.print(temp.substring(i, i + 1) + " ");
    }
    System.out.println();
    String num[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
    String unit[] = { "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟" };

    len = temp.length();
    if (temp == null || "".equals(temp) || temp.length() <= 0) {
       System.out.println("Input is null");
    } else {
     //(==-1)说明
     dot = temp.indexOf(".");
          if(dot == -1){          
         int i = 0;
       // len-1这个注意,i在前面初始化为0
       for (i = 0; i < len - 1; i++) {
        ivalue = Integer.parseInt(temp.substring(i, i + 1));
        retStrBuf.append(num[ivalue] + (unit[(len - i - 1) % 8]));
        // System.out.print(num[ivalue]+(unit[(len - i - 1) % 8]));
       }
       // 单独取最后一位数
       ivalue = Integer.parseInt(String.valueOf(temp.substring(i, i + 1)));
       // 最后一位为零,将不作处理
       if (ivalue != 0) {
        retStrBuf.append(num[ivalue]);
       }
       retStrBuf.append("元整");
       System.out.println(retStrBuf.toString());
       // System.out.println(","+num[ivalue]);
          }else{
              String tmpStr1 = temp.substring(0, dot);
              len = tmpStr1.length();
              int i = 0;
         // len-1这个注意,i在前面初始化为0
         for (i = 0; i < len - 1; i++) {
          ivalue = Integer.parseInt(temp.substring(i, i + 1));
          retStrBuf.append(num[ivalue] + (unit[(len - i - 1) % 8]));
          // System.out.print(num[ivalue]+(unit[(len - i - 1) % 8]));
         }
         // 单独取最后一位数
         ivalue = Integer.parseInt(String.valueOf(temp.substring(i, i + 1)));
         // 最后一位为零,将不作处理
         if (ivalue != 0) {
          retStrBuf.append(num[ivalue]);
         }
       
         //跟整数处理不一样的部分
            if (dot < temp.length() - 1)  {
                retStrBuf.append("点");
                //从小数点后一位开始取
                String tmpStr2 = temp.substring(dot + 1);
                len = tmpStr2.length();
                //这里不需要len-1
                for (i = 0; i < len; i++)  {
                    ivalue = Integer.parseInt(tmpStr2.substring(i, i + 1));
                    retStrBuf.append(num[ivalue]);
                }
            }
          
         retStrBuf.append("元整");
       
            System.out.println(retStrBuf.toString());
          }
    }

    }
}

你可能感兴趣的:(java)