一个简单的将数字转化为汉字大写的方法

package com.abc.ebank.util;

/**
 * @author wangshuangli
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class Digit2ChineseNumber {

 /**
  * Constructor for Digit2ChineseNumber.
  */
 public Digit2ChineseNumber() {
  super();
 }
 /**将金额小写转化为大写
  * Method Number2Cap.
  * @param amount
  * @return String
  * 首先将小写数字转化为大写汉字,中间不作其他事情
  * 然后再在其中添加"万"等东西;
  */
 public static String Number2Cap(String amount) {

  amount = Num2ChCap(amount);

  int decimal_address; //小数点的位置如果没有小数点则值为数值长度加1;,有小数点则为小数点所在位置
  String s_decimal = "";
  if (amount.indexOf('.') != -1) { //有小数点的情况
   decimal_address = amount.indexOf('.');
   s_decimal = amount.substring(decimal_address + 1);
   if (s_decimal.trim().equals("零零")) {
    s_decimal = "";
   } else if (s_decimal.trim().equals("零")) {
    s_decimal = "";
   } else {
    s_decimal = DecAddUnit(s_decimal);
   }
  } else {
   decimal_address = amount.length() + 1;
  }
  String s_Int = amount.substring(0, decimal_address);
  s_Int = IntAddUnit(s_Int);
  s_Int = Coordinate_String(s_Int); //整理字符串将一些多余的东西去除
  return s_Int + s_decimal + "整";
 }

 /**整理字符串将一些多余的东西去除如零元零万等
  * Method Coordinate_String.
  * @param s_Int
  * @return String
  */
 private static String Coordinate_String(String s_Int) {
  int address;
  //for循环去除零
  for (; true;) {
   address = s_Int.indexOf("零万");
   if (address == -1) {
    address = s_Int.indexOf("零仟");
   }
   if (address == -1) {
    address = s_Int.indexOf("零佰");
   }
   if (address == -1) {
    address = s_Int.indexOf("零拾");
   }
   if (address == -1) {
    address = s_Int.indexOf("零亿");
   }
   if (address == -1) {
    address = s_Int.indexOf("零元");
   }
   if (address == -1)
    break;
   System.err.println(s_Int.substring(0, address));
   System.err.println(s_Int.substring(address + 2));
   s_Int = s_Int.substring(0, address) + s_Int.substring(address + 1);
  }
  String tmp = "@亿@万@千@佰@拾亿万亿仟亿佰亿拾万仟万佰万拾仟佰仟拾佰拾";
  s_Int = "@" + s_Int;
  for (int i = 0; i < tmp.length();) {
   String tmp1 = tmp.substring(i, i + 2);
   address = 0;
   for (; true;) {
    address = s_Int.indexOf(tmp1);
    if (address == -1)
     break;
    System.err.println(s_Int.substring(0, address + 1));
    System.err.println(s_Int.substring(address + 2));
    s_Int =
     s_Int.substring(0, address + 1)
      + s_Int.substring(address + 2);
   }
   i += 2;
  }
  return s_Int.substring(1);
 }

 /**添加万仟佰拾亿仟佰拾万仟佰拾元到字符串对应位置
  * Method IntAddUnit.
  * @param s_Int
  * @return String
  */
 private static String IntAddUnit(String s_Int) {
  String Units = "元拾佰仟万拾佰仟亿拾佰仟万";

  s_Int = StringOverTurn(s_Int);
  int s_Int_length = s_Int.length();
  String tmp = "";
  for (int i = 0; i < s_Int_length; i++) {
   tmp =
    tmp
     + String.valueOf(Units.charAt(i))
     + String.valueOf(s_Int.charAt(i));
  }
  tmp = StringOverTurn(tmp);
  return tmp;
 }

 /**字符串翻转将从abcdefg翻转为gfedcba
  * Method StringOverTurn.
  * @param s_Int
  * @return String
  */
 private static String StringOverTurn(String s_Int) {
  ;
  int s_Int_length = s_Int.length();
  char[] Overtmp = s_Int.toCharArray();
  String tmp = "";
  for (int i = s_Int_length - 1; i > -1; i--) {
   tmp = tmp + Overtmp[i];
  }
  return tmp;
 }

 /**将角分加入字符串对应位置中
  * Method DecAddUnit.
  * @param s_decimal
  * @return String
  */
 private static String DecAddUnit(String returnString) {

  switch (returnString.length()) {
   //小数长度为1表示只有角无分
   case 1 :
    returnString =
     returnString.substring(0, 1)
      + "角"
      + returnString.substring(2);

    break;
    //小数长度为2表示有角有分
   case 2 :
    returnString =
     returnString.substring(0, 1)
      + "角"
      + returnString.substring(1, 2)
      + "分";

    break;
  } //end of swtich
  return returnString;
 }

 /**将数字转换为中文大写
  * 返回格式为壹贰.壹贰
  * Method Num2ChCap.
  * @param amount
  * @return String
  */
 private static String Num2ChCap(String amount) {
  int length = amount.length();
  char CapNumber;
  String returnString = "";
  for (int i = 0; i < length; i++) {
   CapNumber = amount.charAt(i);
   switch (CapNumber) {
    case '0' :
     returnString = returnString + "零";
     break;
    case '1' :
     returnString = returnString + "壹";
     break;
    case '2' :
     returnString = returnString + "贰";
     break;
    case '3' :
     returnString = returnString + "叁";
     break;
    case '4' :
     returnString = returnString + "肆";
     break;
    case '5' :
     returnString = returnString + "伍";
     break;
    case '6' :
     returnString = returnString + "陆";
     break;
    case '7' :
     returnString = returnString + "柒";
     break;
    case '8' :
     returnString = returnString + "捌";
     break;
    case '9' :
     returnString = returnString + "玖";
     break;
    case '.' :
     returnString = returnString + ".";
     break;
   } //end of case;
  } //end of for;
  return returnString;

 }

// public static void main(String[] args) {
// }
}

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭