gpt4 book ai didi

java - 如何将数字转换为单词错误输出为十万而不是千

转载 作者:行者123 更新时间:2023-11-30 02:36:22 27 4
gpt4 key购买 nike

我正在创建一个将数字转换为单词的程序。该程序工作正常,但如果“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;
}

关于java - 如何将数字转换为单词错误输出为十万而不是千,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42953846/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com