Java implementation of the method of digital conversion to English _java

Source: Internet
Author: User

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:

English numerals, by 3-digit 3-bit distinction

hundred:100
thousand:1,000
million:1,000,000
billion:1,000,000,000
trillion:1,000,000,000,000
quintillion:1,000,000,000,000,000,000
sextillion:1,000,000,000,000,000,000,000
nonillion:1,000,000,000,000,000,000,000,000,000,000
Centillion:1 followed by 303 zeros

So the numeric string is divided into 3 bits, respectively, to parse

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.