Rumah >Java >javaTutorial >Adakah perpustakaan matematik standard Java menawarkan fungsi untuk menukar nombor kepada perwakilan perkataan mereka?

Adakah perpustakaan matematik standard Java menawarkan fungsi untuk menukar nombor kepada perwakilan perkataan mereka?

Mary-Kate Olsen
Mary-Kate Olsenasal
2024-12-30 11:14:19401semak imbas

Does Java's standard math library offer a function to convert numbers to their word representations?

Cara menukar nombor kepada perkataan dalam java

Latar Belakang

Pada masa ini, mekanisme asas tersedia untuk menukar nombor kepada perkataan menggunakan tatasusunan statik dan menterjemahkannya berdasarkan saiz nombor ke dalam teks bahasa Inggeris. Walau bagaimanapun, pendekatan ini menghadapi cabaran dengan jumlah yang sangat besar.

<br>10183 = Sepuluh ribu seratus lapan puluh tiga<br>90 = Sembilan puluh<br>5888 = Lima ribu lapan ratus lapan puluh lapan<br>

Soalan

Adakah terdapat fungsi mudah dalam mana-mana perpustakaan matematik yang boleh digunakan untuk tujuan ini?

Jawapan

Ini adalah kodnya, saya rasa tiada kaedah di dalamnya SE.

Ia pada asasnya menukar nombor kepada rentetan, menghuraikannya dan mengaitkannya dengan beratnya.

Sebagai contoh

1000

1 dianggap sebagai seribu kedudukan, dan 1 dipetakan kepada "satu" dan seribu kerana kedudukannya.

Berikut ialah kod dari tapak web:

Bahasa Inggeris


import java.text.DecimalFormat;

kelas awam EnglishNumberToWords {

String akhir statik peribadi[] tensNames = {

"",
" ten",
" twenty",
" thirty",
" forty",
" fifty",
" sixty",
" seventy",
" eighty",
" ninety"

};

String akhir statik peribadi[] numNames = {

"",
" one",
" two",
" three",
" four",
" five",
" six",
" seven",
" eight",
" nine",
" ten",
" eleven",
" twelve",
" thirteen",
" fourteen",
" fifteen",
" sixteen",
" seventeen",
" eighteen",
" nineteen"

};

peribadi EnglishNumberToWords() {}

penukar String statik peribadiLessThanOneThousand(nombor int) {

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;

}

penukaran String statik awam(nombor panjang) {

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

}

/**

  • ujian
  • @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
 **/

}
}

Français
agak berbeza daripada versi bahasa inggeris tetapi bahasa perancis banyak lebih sukar!


pakej com.rgagnon.howto;

import java.text.*;

class FrenchNumberToWords {
String akhir statik peribadi[] dizaineNames = {

"",
"",
"vingt",
"trente",
"quarante",
"cinquante",
"soixante",
"soixante",
"quatre-vingt",
"quatre-vingt"

} ;

Rentetan akhir statik peribadi[] uniteNames1 = {

"",
"un",
"deux",
"trois",
"quatre",
"cinq",
"six",
"sept",
"huit",
"neuf",
"dix",
"onze",
"douze",
"treize",
"quatorze",
"quinze",
"seize",
"dix-sept",
"dix-huit",
"dix-neuf"

};

Rentetan akhir statik peribadi[] uniteNames2 = {

"",
"",
"deux",
"trois",
"quatre",

Atas ialah kandungan terperinci Adakah perpustakaan matematik standard Java menawarkan fungsi untuk menukar nombor kepada perwakilan perkataan mereka?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn