我正在创建一个将数字转换为单词的程序。该程序运行正常,但是在“ 100500”给出错误的输出而不是“一百五十万”的情况下
其显示为“一十万五千”。
程序如下
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
public class CheckRupees {
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;
}
private static String convertLessThanOneThousandForLakhs(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] + " lakh" + 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 = convertLessThanOneThousandForLakhs(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) {
int xValue=200500;
System.out.println(CheckRupees.convert(xValue));
}
}
最佳答案
您应该以更结构化的方式实现您的操作,而不是为每个名称创建一个特殊的情况。此外,当Java具有数值比较运算符时,使用格式和子字符串运算的这些组合毫无意义。
例如。
private static final String[] LARGE_NAMES = {
"billion", "million", "lakh", "thousand", "hundred"
};
private static final int[] LARGE_FACTORS = {
1_000_000_000, 1_000_000, 100_000, 1_000, 100
};
private static final String[] TENS_NAMES = {
"ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"
};
private static final String[] SMALL_NAMES = {
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"
};
public static String toName(int number) {
return number==0? "zero": appendName(number, new StringBuilder()).toString();
}
private static StringBuilder appendName(int number, StringBuilder sb) {
if(number >= LARGE_FACTORS[LARGE_FACTORS.length-1]) {
for(int i = 0; i < LARGE_FACTORS.length; i++) {
int f = LARGE_FACTORS[i];
if(number >= f) {
int quantity = number/f;
appendName(quantity, sb).append(' ').append(LARGE_NAMES[i]);
number -= quantity*f;
}
}
}
if(number>0) {
if(sb.length()>0) sb.append(' ');
if(number>SMALL_NAMES.length) {
int tenth = number/10;
sb.append(TENS_NAMES[tenth-1]);
number -= tenth*10;
if(number == 0) return sb;
sb.append(' ');
}
sb.append(SMALL_NAMES[number-1]);
}
return sb;
}