gpt4 book ai didi

java - 拼写数字到数字?

转载 作者:搜寻专家 更新时间:2023-11-01 02:50:40 26 4
gpt4 key购买 nike

有人知道可以接受非常大的拼写数字并将其转换为数字的 Java 库吗?如果它也可以做小数位就好了。示例.. 10 到 10

最佳答案

嗯,为什么不试试你自己的呢?这里有 2 个示例可以让您开始:

import java.util.*;

public class NumToWords {
String string;
String st1[] = { "", "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", };
String st2[] = { "hundred", "thousand", "lakh", "crore" };
String st3[] = { "ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "ninteen", };
String st4[] = { "twenty", "thirty", "fourty", "fifty", "sixty", "seventy",
"eighty", "ninty" };

public String convert(int number) {
int n = 1;
int word;
string = "";
while (number != 0) {
switch (n) {
case 1:
word = number % 100;
pass(word);
if (number > 100 && number % 100 != 0) {
show("and ");
}
number /= 100;
break;

case 2:
word = number % 10;
if (word != 0) {
show(" ");
show(st2[0]);
show(" ");
pass(word);
}
number /= 10;
break;

case 3:
word = number % 100;
if (word != 0) {
show(" ");
show(st2[1]);
show(" ");
pass(word);
}
number /= 100;
break;

case 4:
word = number % 100;
if (word != 0) {
show(" ");
show(st2[2]);
show(" ");
pass(word);
}
number /= 100;
break;

case 5:
word = number % 100;
if (word != 0) {
show(" ");
show(st2[3]);
show(" ");
pass(word);
}
number /= 100;
break;

}
n++;
}
return string;
}

public void pass(int number) {
int word, q;
if (number < 10) {
show(st1[number]);
}
if (number > 9 && number < 20) {
show(st3[number - 10]);
}
if (number > 19) {
word = number % 10;
if (word == 0) {
q = number / 10;
show(st4[q - 2]);
} else {
q = number / 10;
show(st1[word]);
show(" ");
show(st4[q - 2]);
}
}
}

public void show(String s) {
String st;
st = string;
string = s;
string += st;
}

public static void main(String[] args) {
NumToWords w = new NumToWords();
Scanner input = new Scanner(System.in);
System.out.print("Enter Number: ");
int num = input.nextInt();
String inwords = w.convert(num);
System.out.println(inwords);
}
}

还有这个:

class constNumtoLetter
{
String[] unitdo ={"", " One", " Two", " Three", " Four", " Five",
" Six", " Seven", " Eight", " Nine", " Ten", " Eleven", " Twelve",
" Thirteen", " Fourteen", " Fifteen", " Sixteen", " Seventeen",
" Eighteen", " Nineteen"};
String[] tens = {"", "Ten", " Twenty", " Thirty", " Forty", " Fifty",
" Sixty", " Seventy", " Eighty"," Ninety"};
String[] digit = {"", " Hundred", " Thousand", " Lakh", " Crore"};
int r;


//Count the number of digits in the input number
int numberCount(int num)
{
int cnt=0;

while (num>0)
{
r = num%10;
cnt++;
num = num / 10;
}

return cnt;
}


//Function for Conversion of two digit

String twonum(int numq)
{
int numr, nq;
String ltr=\"";

nq = numq / 10;
numr = numq % 10;

if (numq>19)
{
ltr=ltr+tens[nq]+unitdo[numr];
}
else
{
ltr = ltr+unitdo[numq];
}

return ltr;
}

//Function for Conversion of three digit

String threenum(int numq)
{
int numr, nq;
String ltr = "";

nq = numq / 100;
numr = numq % 100;

if (numr == 0)
{
ltr = ltr + unitdo[nq]+digit[1];
}
else
{
ltr = ltr +unitdo[nq]+digit[1]+" and"+twonum(numr);
}
return ltr;

}

}

class originalNumToLetter

{

public static void main(String[] args) throws Exception
{

//Defining variables q is quotient, r is remainder

int len, q=0, r=0;
String ltr = " ";
String Str = "Rupees";
constNumtoLetter n = new constNumtoLetter();
int num = Integer.parseInt(args[0]);

if (num <= 0) System.out.println(\"Zero or Negative number not for conversion");

while (num>0)
{

len = n.numberCount(num);

//Take the length of the number and do letter conversion

switch (len)

{
case 8:
q=num/10000000;
r=num%10000000;
ltr = n.twonum(q);
Str = Str+ltr+n.digit[4];
num = r;
break;

case 7:
case 6:
q=num/100000;
r=num%100000;
ltr = n.twonum(q);
Str = Str+ltr+n.digit[3];
num = r;
break;

case 5:
case 4:

q=num/1000;
r=num%1000;
ltr = n.twonum(q);
Str= Str+ltr+n.digit[2];
num = r;
break;

case 3:


if (len == 3)
r = num;
ltr = n.threenum(r);
Str = Str + ltr;
num = 0;
break;

case 2:

ltr = n.twonum(num);
Str = Str + ltr;
num=0;
break;

case 1:
Str = Str + n.unitdo[num];
num=0;
break;
default:

num=0;
System.out.println(\"Exceeding Crore....No conversion");
System.exit(1);


}
if (num==0)
System.out.println(Str+\" Only");
}

}

}

编辑:

这个样本看起来会转换成数十亿:

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", " ");
}

/**
* testing
* @param args
*/
public static void main(String[] args) {
System.out.println("*** " + EnglishNumberToWords.convert(0));
System.out.println("*** " + EnglishNumberToWords.convert(1));
System.out.println("*** " + EnglishNumberToWords.convert(16));
System.out.println("*** " + EnglishNumberToWords.convert(100));
System.out.println("*** " + EnglishNumberToWords.convert(118));
System.out.println("*** " + EnglishNumberToWords.convert(200));
System.out.println("*** " + EnglishNumberToWords.convert(219));
System.out.println("*** " + EnglishNumberToWords.convert(800));
System.out.println("*** " + EnglishNumberToWords.convert(801));
System.out.println("*** " + EnglishNumberToWords.convert(1316));
System.out.println("*** " + EnglishNumberToWords.convert(1000000));
System.out.println("*** " + EnglishNumberToWords.convert(2000000));
System.out.println("*** " + EnglishNumberToWords.convert(3000200));
System.out.println("*** " + EnglishNumberToWords.convert(700000));
System.out.println("*** " + EnglishNumberToWords.convert(9000000));
System.out.println("*** " + EnglishNumberToWords.convert(9001000));
System.out.println("*** " + EnglishNumberToWords.convert(123456789));
System.out.println("*** " + EnglishNumberToWords.convert(2147483647));
System.out.println("*** " + EnglishNumberToWords.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
**/
}
}

引用资料:

关于java - 拼写数字到数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11817838/

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