欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现阿拉伯数字转汉字数字

程序员文章站 2024-02-23 22:08:58
本文实例为大家分享了java实现阿拉伯数字转汉字数字的具体代码,供大家参考,具体内容如下 package bin; /** * @author elsa...

本文实例为大家分享了java实现阿拉伯数字转汉字数字的具体代码,供大家参考,具体内容如下

package bin;

/**
 * @author elsa
 * @param args
 * @param 2013-1-21
 */
public class doubletest {
  static string[] units = { "", "十", "百", "千", "万", "十万", "百万", "千万", "亿",
      "十亿", "百亿", "千亿", "万亿" };
  static char[] numarray = { '零', '一', '二', '三', '四', '五', '六', '七', '八', '九' };

  /**
   * 测试
   * @param args
   */
  public static void main(string[] args) {
    int num = 234234324;
    string numstr = foematinteger(num);
    system.out.println("num= " + num + ", convert result: " + numstr);
    double decimal = 12312;
    system.out
        .println("============================================================");
    string decstr = formatdecimal(decimal);
    system.out.println("decimal= " + decimal + ", decstr: " + decstr);
  }

  private static string foematinteger(int num) {
    char[] val = string.valueof(num).tochararray();
    int len = val.length;
    stringbuilder sb = new stringbuilder();
    for (int i = 0; i < len; i++) {
      string m = val[i] + "";
      int n = integer.valueof(m);
      boolean iszero = n == 0;
      string unit = units[(len - 1) - i];
      if (iszero) {
        if ('0' == val[i - 1]) {
          continue;
        } else {
          sb.append(numarray[n]);
        }
      } else {
        sb.append(numarray[n]);
        sb.append(unit);
      }
    }
    return sb.tostring();
  }

  /**
   * 对于double类型的的装换
   * 
   * @param decimal
   * @return
   */
  private static string formatdecimal(double decimal) {
    string decimals = string.valueof(decimal);
    int decindex = decimals.indexof(".");
    int integ = integer.valueof(decimals.substring(0, decindex));
    int dec = integer.valueof(decimals.substring(decindex + 1));
    string result = foematinteger(integ) + "." + formatfractionalpart(dec);
    return result;
  }

  /**
   * 对整数类型的转换
   * 
   * @param decimal
   * @return
   */
  private static string formatfractionalpart(int decimal) {
    char[] val = string.valueof(decimal).tochararray();
    int len = val.length;
    stringbuilder sb = new stringbuilder();
    for (int i = 0; i < len; i++) {
      int n = integer.valueof(val[i] + "");
      sb.append(numarray[n]);
    }
    return sb.tostring();
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。