将中文数字转换成英文表示

public class NumberToWords {

  private final String[] tensNames = {
    "",
    " ten",
    " twenty",
    " thirty",
    " forty",
    " fifty",
    " sixty",
    " seventy",
    " eighty",
    " ninety"
  };

  private final String[] numNames = {
    "",
    " one",
    " two",
    " three",
    " four",
    " five",
    " six",
    " seven",
    " eight",
    " nine",
    " ten",
    " eleven",
    " twelve",
    " thirteen",
    " fourteen",
    " fifteen",
    " sixteen",
    " seventeen",
    " eighteen",
    " nineteen"
  };

  private String convertLessThanOneThousand(int number) {
    String soFar;

    if (number % 100 < 20){
      soFar = numNames[number % 100];
      number /= 100;
    }else {
      soFar = numNames[number % 10];
      number /= 10;

      if(soFar.equals("")){
    	  soFar = tensNames[number % 10] + soFar;
      }else{
    	  soFar = tensNames[number % 10] + " and " + soFar;
      }
      number /= 10;
    }
    if (number == 0){
    	return soFar;
    }
    return numNames[number] + " hundred" + soFar;
  }

  public String convert(long number) {
    // 0 to 999 999 999 999
    if (number == 0) { return "zero"; }

    String snumber = Long.toString(number);

    // pad with "0"
    String mask = "000000000000";
    DecimalFormat df = new DecimalFormat(mask);
    snumber = df.format(number);

    // XXXnnnnnnnnn
    int billions = Integer.parseInt(snumber.substring(0,3));
    // nnnXXXnnnnnn
    int millions  = Integer.parseInt(snumber.substring(3,6));
    // nnnnnnXXXnnn
    int hundredThousands = Integer.parseInt(snumber.substring(6,9));
    // nnnnnnnnnXXX
    int thousands = Integer.parseInt(snumber.substring(9,12));    

    String tradBillions;
    switch (billions) {
    case 0:
      tradBillions = "";
      break;
    case 1 :
      tradBillions = convertLessThanOneThousand(billions)
      + " billion ";
      break;
    default :
      tradBillions = convertLessThanOneThousand(billions)
      + " billion ";
    }
    String result =  tradBillions;

    String tradMillions;
    switch (millions) {
    case 0:
      tradMillions = "";
      break;
    case 1 :
      tradMillions = convertLessThanOneThousand(millions)
      + " million ";
      break;
    default :
      tradMillions = convertLessThanOneThousand(millions)
      + " million ";
    }
    result =  result + tradMillions;

    String tradHundredThousands;
    switch (hundredThousands) {
    case 0:
      tradHundredThousands = "";
      break;
    case 1 :
      tradHundredThousands = "one thousand ";
      break;
    default :
      tradHundredThousands = convertLessThanOneThousand(hundredThousands)
      + " thousand ";
    }
    result =  result + tradHundredThousands;

    String tradThousand;
    tradThousand = convertLessThanOneThousand(thousands);
    result =  result + tradThousand;

    // remove extra spaces!
    return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");
  }

  /**
   * testing
   * @param args
   */
  public static void main(String[] args) {
	  NumberToWords nw = new NumberToWords();

	  System.out.println("*** " + nw.convert(1031));
	  System.out.println("*** " + nw.convert(1030));
    System.out.println("*** " + nw.convert(0));
    System.out.println("*** " + nw.convert(1));
    System.out.println("*** " + nw.convert(36));
    System.out.println("*** " + nw.convert(136));
    System.out.println("*** " + nw.convert(1030));
    System.out.println("*** " + nw.convert(1036));
    System.out.println("*** " + nw.convert(1236));

    System.out.println("*** " + nw.convert(3000000010L));

    /*
     *** zero
     *** one
     *** sixteen
     *** one hundred
     *** one hundred eighteen
     *** two hundred
     *** two hundred nineteen
     *** eight hundred
     *** eight hundred one
     *** one thousand three hundred sixteen
     *** one million
     *** two millions
     *** three millions two hundred
     *** seven hundred thousand
     *** nine millions
     *** nine millions one thousand
     *** one hundred twenty three millions four hundred
     **      fifty six thousand seven hundred eighty nine
     *** two billion one hundred forty seven millions
     **      four hundred eighty three thousand six hundred forty seven
     *** three billion ten
     **/
  }
}

时间: 2024-12-20 05:35:19

将中文数字转换成英文表示的相关文章

中文数字转换成阿拉伯数字(一千二百三十四万五千六百七十八--&gt;12345678)

昨天老大问我又没有写过中文数字转换成阿拉伯数字,我说没有,我说那应该简单啊,就是将中文对应的数字换成阿拉伯数字就好了啊,比如一就换成1,二就换成2…十换成10.可是那么问题来了…… 一十二呢…不能是1102吧…这不就坑爹了吗?一百万呢………所有我苦苦思索,花费了我差不多半天的时间,终于写出了下面的程序. 1 public static void main(String[] args){ 2 3 Map<Character, String> numberMap = new HashMap<

js 数字转换成英文字母

将 数字转换成相对应的英文字母, 例如:1 - - >  A  2 - - >  B 3 - - >  C 我用到的是 ASCII码,将数字转成ascii相对应的十进制就可以了 JavaScript : String.fromCharCode( number ); 示例: var str=[]; for (var i=0 ;i<26 ; i++){ str.push(String.fromCharCode(i+65)); } console.log(str); 原文地址:https

数字转换成英文 金额转成英文大写

/****** Object: UserDefinedFunction [dbo].[NumberToStr] Script Date: 03/20/2017 23:38:14 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO --创建函数 ALTER FUNCTION [dbo].[NumberToStr] ( @ad_input decimal(38,17) ) RETURNS nvarchar(max) AS BEGIN de

在C#中将数字转换成中文

上篇我们讲了在MSSQL中将数字转换成中文,这篇我们讲讲在C#中将数字转换成中文 下篇将讲一下如何将金额转换成中文金额,废话不多说,具体代码如下: /// <summary> /// 数字转中文 /// </summary> /// <param name="number">eg: 22</param> /// <returns></returns> public string NumberToChinese(in

java将中文转换成英文并支持首字母大写

原文:java将中文转换成英文并支持首字母大写 源代码下载地址:http://www.zuidaima.com/share/1563120428403712.htm 之前看到有牛牛发布过这一个代码,但是他写的那一个代码只是纯转换,并不能满足太多的需求.今天正好工作时遇到了这一个问题,需求把首字母变为大写.参考了一些代码演变而成的.希望大家以后可以用到. 这是事例图 这是结果图. 版权声明:本文为博主原创文章,未经博主允许不得转载.

算法题--将英文数字转换成阿拉伯数字

将英文数字转换成阿拉伯数字 随机看了几道算法题,以下答案也是别人写的,非本人所写 思路: 三个进行一个划分 代码: public class POJ_2121 { public static void main(String[] args) { /** * negative, zero, one, two, three, four, five, six, seven, * eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen,

数字转换成中文大小写、金额大小写

将数字转换成中文大小写.金额大小写  /// <summary> /// 将数字转换成中文大写 /// </summary> /// <param name="Value">数字</param> /// <param name="bToUpper">是否转换成汉字大写 true表示大写 false表示小写</param> /// <param name="bMoney"

NFA转换成DFA——汉字形式数字转换成整数数字

偶然间遇到了一个需求:汉字形式数字转换成整数数字.如果不处理意外情况,可以写的很简单(比如不会出现三三等),详情可以看这里.但是,想着可以写成一个FA的形式,于是乎,发现并不是想象中的那么简单..因为写成FA就发现,要处理非法形式的问题,还是有点麻烦的. 好不容易写成了FA,发现是个NFA,于是乎写了个NFA转换成DFA的代码,也支持了一套简单的FA的定义规则.代码如下: package ie; import java.util.ArrayList; import java.util.HashM

C#字母转换成数字/数字转换成字母 - ASCII码转换

字母转换成数字 byte[] array = new byte[1];   //定义一组数组arrayarray = System.Text.Encoding.ASCII.GetBytes(string); //string转换的字母int asciicode = (short)(array[0]); ASCII码 = Convert.ToString(asciicode); //将转换一的ASCII码转换成string型 数字转换成字母byte[] array = new byte[1];ar