The example of this article describes the Java implementation of the method of digital conversion into English. Share to everyone for your reference. The specific analysis is as follows:
public class Numutil {public static final string[] Ennum = {//Basic numeral table "zero", "one", "tow", "three", "four", "Five", " Six "," seven "," eight "," Nine "," Ten "," Eleven "," Twelve "," Thirteen "," fourteen "," fifteen "," Sixteen "," Seventeen ", "Eighteen", "Nineteen", "Twenty", "", "", "", "", "", "", "", "", "" Thirty "," "," "," "," "," "," "," "," "," "," "," "," "," " Ourty "," "," "," "," "," "," "," "", "", "", "Fifty," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ""
"", "", "", "", "Seventy," "," "," "," "," "," "," "," "," "," Eighty, "", "", "", "", "", "", "", "", "", "", "", "", "", "" public static final string[] Enunit = {"Hundred", "thousand", "million", "billion", "trillion", "quintillion"}; Unit table public static void main (string[] args) {System.out.println (analyze (1));//test Data SYSTEM.OUT.PRINTLN (analyze (21))
;
SYSTEM.OUT.PRINTLN (Analyze (105));
SYSTEM.OUT.PRINTLN (Analyze (3250));
SYSTEM.OUT.PRINTLN (Analyze (47826));
SYSTEM.OUT.PRINTLN (Analyze (56945781)); public static String analyze (long num) {//long parameter, return analyze (string.valueof (num)); Because the long type has a limit, the string parameter method is the primary} public static string analyze (String num) {//numeric string parameter//judge whether the string is a number if (!num.matches) ("\
D+ ")) {return String.Format ('%s is not number ', num);
num = Num.replaceall ("^[0]* ([1-9]*)", "$");
Remove the 0 in front of the string if (num.length () = = 0) {//If the length is 0, the original string is 0 return ennum[0];
If Else if (Num.length () > 9) {//If greater than 9, that is greater than 999999999, the topic limits the condition return "too big"; ///By 3-bit split Group int count = (num.length ()% 3 = 0)?
Num.length ()/3:num.length ()/3 + 1;
if (Count > Enunit.length) {return ' too big ';
//Judge whether the unit is over,//can be appropriately appended enunit string[] group = new String[count];
for (int i = Num.length (), j = Group.length-1 i > 0; I-= 3) {group[j--] = num.substring (Math.max (i-3, 0), i); } StringBuilder buf = new StringBuilder ();
The result is saved for (int i = 0; i < count; i++) {//traversal split Group int v = integer.valueof (group[i]); if (v >= 100) {//BecauseSplit by 3 bits, so there won't be more than 999 buf.append (ennum[v/100]). Append (""). Append (Enunit[0)). Append (""); v = v% 100;
Gets the hundred, and gets the number of if (v!= 0) {buf.append ("and") after the hundred;
//If the number after the hundred is not 0, append and} if (v!= 0) {//if V is not 0 to parse if (v < | | v% 10 = 0) {//if less than 20 or 10 integer times, directly take the words of the basic numeral table
Buf.append (Ennum[v]). Append ("");
else {//otherwise take 10 digits, and then take a single-digit numeral buf.append (ennum[v-v%)). Append ("");
Buf.append (ennum[v%]). Append ("");
} if (I!= count-1) {//More than hundred groups append the corresponding unit buf.append (enunit[count-1-i)). Append (""); }} return Buf.tostring (). Trim ();
Return value}}
I hope this article will help you with your Java programming.