目前,已經有一個基本的機制可以使用靜態陣列將數字轉換為單詞,根據數字的大小將它們翻譯成英文文本。然而,這種方法遇到了異常大的數字的挑戰。
<br>10183 = 一萬一百八十三<br>90 = 九十<br>5888 = 五千八百八十八<br>
數學庫中是否有一個簡單的函數可以用於此目的?
這是程式碼,我認為SE中沒有任何方法。
它本質上是將數字轉換為字串,解析它,並將其與其權重關聯起來。
例如
<br>1000<br>< ;/pre><p>1被視為千位,1映射到由於其位置,“一”和千。 </p><p>這是來自網站的代碼:</p><p><strong>英語</strong></p><p></p><pre class="brush:php;toolbar:false"><br> import java.text.DecimalFormat;<p>公共類別EnglishNumberToWords {</p><p>私有靜態最終字串[]tensNames = {</p><pre class="brush:php;toolbar:false">"", " ten", " twenty", " thirty", " forty", " fifty", " sixty", " seventy", " eighty", " ninety"
};
私有靜態最終字串[]numNames = {
"", " one", " two", " three", " four", " five", " six", " seven", " eight", " nine", " ten", " eleven", " twelve", " thirteen", " fourteen", " fifteen", " sixteen", " seventeen", " eighteen", " nineteen"
};
私人EnglishNumberToWords() {}
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", " ");
}
/**
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
與英文版有很大不同,但法文版很多更多的困難!
<br>package com.rgagnon.howto;<p>導入java.text.*;</p><p>class FrenchNumberToWords {<br> private static final[ ] dizaineNames = {</p><pre class="brush:php;toolbar:false">"", "", "vingt", "trente", "quarante", "cinquante", "soixante", "soixante", "quatre-vingt", "quatre-vingt"
};
私人靜態最終字串[]uniteNames1 = {
"", "un", "deux", "trois", "quatre", "cinq", "six", "sept", "huit", "neuf", "dix", "onze", "douze", "treize", "quatorze", "quinze", "seize", "dix-sept", "dix-huit", "dix-neuf"
};
最終靜態字串最終[] unityNames2 = {
"", "", "deux", "trois", "quatre",
以上是Java 的標準數學函式庫是否提供將數字轉換為其文字表示形式的函數?的詳細內容。更多資訊請關注PHP中文網其他相關文章!