The example of this article tells the way Java realizes numbers into English. Share it for everyone for your reference. The specific analysis is as follows:
English Digital, distinguished by 3 -bit and 3 digits
Hundred: 100thousand: 1,000million: 1,000,000billion: 100,000,000,000TRILLION: 100,000,000,000 QUINTILION: 100,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000. 0,000,000,000,000NONILLION: 100,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000Centillion: 1 FOLLOWED by 303 Zeros
Therefore
Public class numutil {public static final string [] ennum = {// basic number table "zero", "one", "tow", "three", "five", "six", "seven" " , "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", " twenty "," "," "," "," "," "," "," "," "," "," "," "," ",", "", "", " "," "," "," "," "", "", "", "", "", "", "", "", "", "", "", "", " "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" "" "" "" "", "seventy", "", "", "", "", "", "", "", "", "", "", "", "", "" "" "" " , "", "", "", "", "", "ninety"}; Public Static final string [] enunit = {"hundred", "thisand", "million", "bition", "trillion", "quintillion" }; // 单位表public static void main(String[] args) { System.out.println(analyze(1)); // 测试数据System.out.println(analyze(21)); System. Out.println (Analyze (105)); System.out.println (Analyze (3250)); System.out.println (Analyze (47826)); 1);} Public Static String Analyze (long num) {// Long type parameters, Return Analyze (String.valueof (Num)); // Because the LONG type has the limit, the string parameter method is the main} Public String Analyze { / / / / / / / / /Digital string parameters // Determine whether the strings are digital if (! Num.matches ("// d+") {Return String.Format ("%S IS Not Number", Num); ("^[0]*([1-9]*)", "$ 1"); // Remove the if (num.length () == 0) {// if the length of 0 in front of the strings is 0, if the length is 0, Then the original string is 0 Return Ennum [0];} Else if (num.length ()> 9) {// If it is greater than 9, that is, it is greater than 999999999, the topic restrictions Return "too big"; The division group int count = (num.length () % 3 == 0)? Num.length () / 3: num.length () / 3 + 1; if (count> enunit.length) {Return "too big" ;} // Determine whether the unit unit exceeds, // can appropriately add 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 (); // result saved for (int i = 0; i <count; i ++) {// The group int v = Integer.Valueof (group [i]); if (v> = 100) BUF.APPPEND (ennum [v / 100]). APPEND ("") .appnd (enunit [0]) .appnd (""). Hundreds after the number if (v! = 0) {buf.append ("and");} // If the number of hundreds after a hundred units is not 0, then add and} if (v! = 0) {// Prerequisite It is not that V is not 0 to analyze if (v <20 || v % 10 == 0) {// If it is an integer multiple of less than 20 or 10, directly take the word word of the basic number table buf.append (ennum [v] ) .appnd ("");} Else {// Otherwise, take 10 digits, and then take a digit digital bUF.APPEND (ennum [v -v % 10]). ennum [v % 10]). APPEND ("");} if (i! = Count -1) {// More than a hundred or more groups to add corresponding units buf.append (enunit [Count -1 -I]). Append ("");}}} Return buf.tostring (). trim (); // Return value}}}
It is hoped that this article is helpful to everyone's Java program design.