我们目前有一个粗略的机制来将数字转换为单词(例如,使用一些静态数组),并基于数字的大小将其转换为英语文本。但是,对于庞大的数字,我们遇到了问题。
10183 = Ten thousand one hundred eighty three
90 = Ninety
5888 = Five thousand eight hundred eighty eight
在任何一个数学库中有没有一个简单易用的函数,我可以使用它来达到这个目的?
发布于 2010-10-12 17:08:35
ICU4J包含很好的数字拼写支持。带有“规则”的文件可以很容易地编辑,添加其他语言也没有问题(我们这样做了,例如波兰语和俄语)。
发布于 2013-09-11 21:49:57
我认为这个解决方案不是最好的,因为它只适用于int
,但我认为它对初学者来说很棒。
public class NumberWordConverter {
public static final String[] units = {
"", "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
};
public static final String[] tens = {
"", // 0
"", // 1
"twenty", // 2
"thirty", // 3
"forty", // 4
"fifty", // 5
"sixty", // 6
"seventy", // 7
"eighty", // 8
"ninety" // 9
};
public static String convert(final int n) {
if (n < 0) {
return "minus " + convert(-n);
}
if (n < 20) {
return units[n];
}
if (n < 100) {
return tens[n / 10] + ((n % 10 != 0) ? " " : "") + units[n % 10];
}
if (n < 1000) {
return units[n / 100] + " hundred" + ((n % 100 != 0) ? " " : "") + convert(n % 100);
}
if (n < 1000000) {
return convert(n / 1000) + " thousand" + ((n % 1000 != 0) ? " " : "") + convert(n % 1000);
}
if (n < 1000000000) {
return convert(n / 1000000) + " million" + ((n % 1000000 != 0) ? " " : "") + convert(n % 1000000);
}
return convert(n / 1000000000) + " billion" + ((n % 1000000000 != 0) ? " " : "") + convert(n % 1000000000);
}
public static void main(final String[] args) {
final Random generator = new Random();
int n;
for (int i = 0; i < 20; i++) {
n = generator.nextInt(Integer.MAX_VALUE);
System.out.printf("%10d = '%s'%n", n, convert(n));
}
n = 1000;
System.out.printf("%10d = '%s'%n", n, convert(n));
n = 2000;
System.out.printf("%10d = '%s'%n", n, convert(n));
n = 10000;
System.out.printf("%10d = '%s'%n", n, convert(n));
n = 11000;
System.out.printf("%10d = '%s'%n", n, convert(n));
n = 999999999;
System.out.printf("%10d = '%s'%n", n, convert(n));
n = Integer.MAX_VALUE;
System.out.printf("%10d = '%s'%n", n, convert(n));
}
}
该测试创建了20个随机数,直到Integer.MAX_VALUE
,然后是一些已知有问题的随机数,因为0,10等。输出:
5599908 = 'five million five hundred ninety nine thousand nine hundred eight'
192603486 = 'one hundred ninety two million six hundred three thousand four hundred eighty six'
1392431868 = 'one billion three hundred ninety two million four hundred thirty one thousand eight hundred sixty eight'
1023787010 = 'one billion twenty three million seven hundred eighty seven thousand ten'
1364396236 = 'one billion three hundred sixty four million three hundred ninety six thousand two hundred thirty six'
1511255671 = 'one billion five hundred eleven million two hundred fifty five thousand six hundred seventy one'
225955221 = 'two hundred twenty five million nine hundred fifty five thousand two hundred twenty one'
1890141052 = 'one billion eight hundred ninety million one hundred forty one thousand fifty two'
261839422 = 'two hundred sixty one million eight hundred thirty nine thousand four hundred twenty two'
728428650 = 'seven hundred twenty eight million four hundred twenty eight thousand six hundred fifty'
860607319 = 'eight hundred sixty million six hundred seven thousand three hundred nineteen'
719753587 = 'seven hundred nineteen million seven hundred fifty three thousand five hundred eighty seven'
2063829124 = 'two billion sixty three million eight hundred twenty nine thousand one hundred twenty four'
1081010996 = 'one billion eighty one million ten thousand nine hundred ninety six'
999215799 = 'nine hundred ninety nine million two hundred fifteen thousand seven hundred ninety nine'
2105226236 = 'two billion one hundred five million two hundred twenty six thousand two hundred thirty six'
1431882940 = 'one billion four hundred thirty one million eight hundred eighty two thousand nine hundred forty'
1991707241 = 'one billion nine hundred ninety one million seven hundred seven thousand two hundred forty one'
1088301563 = 'one billion eighty eight million three hundred one thousand five hundred sixty three'
964601609 = 'nine hundred sixty four million six hundred one thousand six hundred nine'
1000 = 'one thousand'
2000 = 'two thousand'
10000 = 'ten thousand'
11000 = 'eleven thousand'
999999999 = 'nine hundred ninety nine million nine hundred ninety nine thousand nine hundred ninety nine'
2147483647 = 'two billion one hundred forty seven million four hundred eighty three thousand six hundred forty seven'
希望它能有所帮助:)
发布于 2017-11-19 19:00:35
package it.tommasoresti.facebook;
class NumbersToWords {
private static final String ZERO = "zero";
private static String[] oneToNine = {
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
};
private static String[] tenToNinteen = {
"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
};
private static String[] dozens = {
"ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"
};
public static String solution(int number) {
if(number == 0)
return ZERO;
return generate(number).trim();
}
public static String generate(int number) {
if(number >= 1000000000) {
return generate(number / 1000000000) + " billion " + generate(number % 1000000000);
}
else if(number >= 1000000) {
return generate(number / 1000000) + " million " + generate(number % 1000000);
}
else if(number >= 1000) {
return generate(number / 1000) + " thousand " + generate(number % 1000);
}
else if(number >= 100) {
return generate(number / 100) + " hundred " + generate(number % 100);
}
return generate1To99(number);
}
private static String generate1To99(int number) {
if (number == 0)
return "";
if (number <= 9)
return oneToNine[number - 1];
else if (number <= 19)
return tenToNinteen[number % 10];
else {
return dozens[number / 10 - 1] + " " + generate1To99(number % 10);
}
}
}
测试
@Test
public void given_a_complex_number() throws Exception {
assertThat(solution(1234567890),
is("one billion two hundred thirty four million five hundred sixty seven thousand eight hundred ninety"));
}
https://stackoverflow.com/questions/3911966
复制相似问题