java数字格式中文化

//内容经供参考,以下代码是运行成功后的结果.切莫copy,拒绝只做理论帝,努力成为手工王

 

public class Number2RMB
{
  private static String[] HanDigiStr = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };

  private static String[] HanDiviStr = { "", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟" };

  public static String convertRMB(String NumStr)
  {
    String RMBStr = "";
    boolean lastZero = false;
    boolean hasValue = false;
    boolean blZero = false;

    int len = NumStr.length();
    if (len > 15)
      return "数值过大!";
    for (int i = len - 1; i >= 0; i--) {
      if (NumStr.charAt(len - i - 1) == ' ')
        continue;
      int n = NumStr.charAt(len - i - 1) - '0';
      if ((n < 0) || (n > 9)) {
        return "输入含非数字字符!";
      }
      if (n != 0) {
        if (lastZero) {
          RMBStr = RMBStr + HanDigiStr[0];
        }

        if ((i % 4 == 3) && (blZero))
          RMBStr = RMBStr + HanDigiStr[0];
        RMBStr = RMBStr + HanDigiStr[n];
        RMBStr = RMBStr + HanDiviStr[i];
        hasValue = true;
      }
      else if ((i % 8 == 0) || ((i % 8 == 4) && (hasValue))) {
        RMBStr = RMBStr + HanDiviStr[i];
      }
      if (i % 8 == 0)
        hasValue = false;
      lastZero = (n == 0) && (i % 4 != 0);
      blZero = (n == 0) && (i % 4 == 0);
    }

    if (RMBStr.length() == 0)
      return HanDigiStr[0];
    return RMBStr;
  }

  public static String convert(double var) {
    String SignStr = "";
    String TailStr = "";

    if (var < 0.0D) {
      var = -var;
      SignStr = "负";
    }
    if ((var > 100000000000000.0D) || (var < -100000000000000.0D)) {
      return "数值位数过大!";
    }
    long temp = Math.round(var * 100.0D);
    long integer = temp / 100L;
    long fraction = temp % 100L;
    int jiao = (int)fraction / 10;
    int fen = (int)fraction % 10;
    String tempStr = String.valueOf(integer);
    int yuan = tempStr.charAt(tempStr.length() - 1) - '0';

    if ((jiao == 0) && (fen == 0))
      TailStr = "整";
    if ((jiao != 0) && (fen == 0))
      if (yuan == 0)
        TailStr = "零" + HanDigiStr[jiao] + "角整";
      else
        TailStr = HanDigiStr[jiao] + "角整";
    if ((jiao == 0) && (fen != 0))
      TailStr = "零" + HanDigiStr[fen] + "分";
    if ((jiao != 0) && (fen != 0)) {
      if (yuan == 0)
        TailStr = "零" + HanDigiStr[jiao] + "角" + HanDigiStr[fen] + "分";
      else {
        TailStr = HanDigiStr[jiao] + "角" + HanDigiStr[fen] + "分";
      }

    }

    return SignStr + convertRMB(String.valueOf(integer)) + "元" + TailStr;
  }
}

你可能感兴趣的:(java数字格式中文化)