在这里,我要问一些奇怪的事情。
我想问一下,是否有任何方法/逻辑可以将整数值转换为包含数字英文单词的字符串值?
例如用户输入22并获得输出二十二或二。
由于
答案 0 :(得分:9)
查看this代码,它可能就是您要找的内容。例如,如果我们有:
,请在main方法中System.out.println(convert(22));
输出:
twenty two
编辑 我已经复制了以下代码,稍微清理了格式(主要方法位于底部):
import java.text.DecimalFormat;
public class EnglishNumberToWords {
private static final String[] tensNames = { "", " ten", " twenty",
" thirty", " forty", " fifty", " sixty", " seventy", " eighty",
" ninety" };
private static final String[] numNames = { "", " one", " two", " three",
" four", " five", " six", " seven", " eight", " nine", " ten",
" eleven", " twelve", " thirteen", " fourteen", " fifteen",
" sixteen", " seventeen", " eighteen", " nineteen" };
private static String convertLessThanOneThousand(int number) {
String soFar;
if (number % 100 < 20) {
soFar = numNames[number % 100];
number /= 100;
} else {
soFar = numNames[number % 10];
number /= 10;
soFar = tensNames[number % 10] + soFar;
number /= 10;
}
if (number == 0)
return soFar;
return numNames[number] + " hundred" + soFar;
}
public static 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", " ");
}
public static void main(String[] args) {
System.out.println(convert(22)); // "twenty two"
}
}
答案 1 :(得分:3)
虽然接受的答案有效,但最好使用已经实现的功能来执行此操作。 ICU4J包含可用于执行此操作的类com.ibm.icu.text.RuleBasedNumberFormat
。它还支持英语以外的语言,以及反向操作,将文本字符串解析为整数值。
这是一个例子,假设我们在类路径中有ICU4J dependency:
import com.ibm.icu.text.RuleBasedNumberFormat;
import java.util.Locale;
RuleBasedNumberFormat nf = new RuleBasedNumberFormat (Locale.UK, RuleBasedNumberFormat.SPELLOUT);
nf.format(24);
// The result is "twenty-four"
答案 2 :(得分:0)
我有一个非常简单的解决方案,可以将java中的整个整数范围转换为hinglish符号
String h1[] = { "Zero", "One", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Eleven", "Tweleve", "Thirteen",
"Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen",
"Nineteen" };
String h2[] = { "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy",
"Eighty", "Ninety" };
String h3[] = { "Hundred", "Thousand", "Lakh", "Crore", "Arab" };
public String parseToHinglishNotation(int num) {
String word = "";
if (num < 0) {
word += "Minus ";
num = -num;
}
if (num < 20) {
word += h1[num];
} else if (num < 100) {
int temp = num / 10 - 2;
word += h2[temp];
temp = num % 10;
if (temp > 0) {
word += " " + parseToHinglishNotation(temp);
}
} else if (num < 1000) {
int temp = num / 100;
word += parseToHinglishNotation(temp) + " " + h3[0];
temp = num % 100;
if (temp > 0) {
word += " " + parseToHinglishNotation(temp);
}
} else if (num < 100000) {
int temp = num / 1000;
word += parseToHinglishNotation(temp) + " " + h3[1];
temp = num % 1000;
if (temp > 0) {
word += " " + parseToHinglishNotation(temp);
}
} else if (num < 10000000) {
int temp = num / 100000;
word += parseToHinglishNotation(temp) + " " + h3[2];
temp = num % 100000;
if (temp > 0) {
word += " " + parseToHinglishNotation(temp);
}
} else if (num < 1000000000) {
int temp = num / 10000000;
word += parseToHinglishNotation(temp) + " " + h3[3];
temp = num % 10000000;
if (temp > 0) {
word += " " + parseToHinglishNotation(temp);
}
} else if (num <= 2147483647) {
int temp = num / 1000000000;
word += parseToHinglishNotation(temp) + " " + h3[4];
temp = num % 1000000000;
if (temp > 0) {
word += " " + parseToHinglishNotation(temp);
}
}
return word;
}
答案 3 :(得分:0)
我知道这篇文章有点陈旧,但我最近提出了自己的解决方案,并认为它可能值得分享。主要功能numToWords
采用1到9999之间的任何Integer
(包括)并输出相应的英语单词,然后是括号中的String
数字。
示例:如果Integer x = 2614;
,numToText(x);
返回"Two Thousand Six Hundred Fourteen (2614)"
:
数字等效词存储在HashMaps中,具体取决于它们是1还是10的倍数(例如6 --> "Six"
,60 --> "Sixty"
),而青少年是用switch
独立处理的言。
import java.util.HashMap;
public class Converter{
public Converter(){
hm1.put(0, "");
hm1.put(1, "One");
hm1.put(2, "Two");
hm1.put(3, "Three");
hm1.put(4, "Four");
hm1.put(5, "Five");
hm1.put(6, "Six");
hm1.put(7, "Seven");
hm1.put(8, "Eight");
hm1.put(9, "Nine");
hm10.put(0, "");
hm10.put(1, "");
hm10.put(2, "Twenty ");
hm10.put(3, "Thirty ");
hm10.put(4, "Fourty ");
hm10.put(5, "Fifty ");
hm10.put(6, "Sixty ");
hm10.put(7, "Seventy ");
hm10.put(8, "Eighty ");
hm10.put(9, "Ninety ");
}
public static String numToWords(Integer x){
// Obtain a char[] of the Integer to analyze each digit.
String s = x.toString();
char[] cArray = s.toCharArray();
// Refer to the length of the Integer as its final index.
int l = cArray.length-1;
String retStr = "";
// The loop counts backwards from the right-most digit.
for(int i = l; i >= 0; i--){
// Determine the numeric value at the left-most index, then move rightward.
// This setup is attributed to the nuances of the English language.
int j = Character.getNumericValue(cArray[l-i]);
//
if(i == 3){
retStr += hm1.get(j);
retStr += " Thousand ";
} else if(i == 2){
retStr += hm1.get(j);
retStr += " Hundred ";
} else if(i == 1){
//If the 10s digit is 1, the final word is 10 <= x <= 19.
if(j == 1){
String tens = "";
// Check the 1s digit to determine x (10 <= x <= 19)
switch(Character.getNumericValue(cArray[l])){
case 0: tens = "Ten";
break;
case 1: tens = "Eleven";
break;
case 2: tens = "Twelve";
break;
case 3: tens = "Thirteen";
break;
case 4: tens = "Fourteen";
break;
case 5: tens = "Fifteen";
break;
case 6: tens = "Sixteen";
break;
case 7: tens = "Seventeen";
break;
case 8: tens = "Eighteen";
break;
case 9: tens = "Nineteen";
break;
}
i = -1; // Ensure it's the last word by indexing out of the loop.
retStr += tens;
} else {
//If the 10s digit is 2 <= x <= 9, the 10s word is 20 <= x <= 90.
retStr += hm10.get(j);
}
} else if(i == 0){
retStr += hm1.get(j);
}
}
return retStr + " (" + x + ") ";
}
private HashMap<Integer, String> hm1 = new HashMap<Integer, String>();
private HashMap<Integer, String> hm10 = new HashMap<Integer, String>();
}