Java implements the method of converting integers into Chinese capital amounts _mssql2008

Source: Internet
Author: User

In daily life, we often convert Arabic numerals into Chinese capitals: "0", "one", "II", "three", "", "" WU "," Lu "," Qi "," ba "," Nine "," Pick Up "," Bai "," Qian "," million "," billion ", so he wrote a class, to achieve the transformation of the function 。
implementation process:
for the implementation of this problem, we first take the number four bits apart (consistent with our habit of reading the numbers), and then to the four digits, make his reading, the specific code is as follows:

private static String getNumberStr1000 (int num) { 
 if (num > 9999 | | | num < 0) {return 
  ""; 
 } 
 int q = num/1000; 
 int b = (num/100)%; 
 int s = (NUM/10)%; 
 int g = num%; 
 StringBuffer sb = new StringBuffer (); 
 Thousand 
 if (q > 0) { 
  sb.append (number1[q]); 
  Sb.append (Number2[3]); 
 Hundred 
 if (b > 0) { 
  sb.append (number1[b]); 
  Sb.append (number2[2]); 
 else { 
  if (q!= 0) { 
   sb.append (number2[0]); 
  } 
 Ten 
 if (S > 0) { 
  sb.append (number1[s]); 
  Sb.append (number2[1]); 
 else { 
  if (b!= 0) { 
   sb.append (number2[0]); 
  } 
 An 
 if (G > 0) { 
  sb.append (number1[g]); 
 } 
 return sb.tostring (); 
} 

Below we only need to write a method to implement each four-digit reading and corresponding units, the specific code is as follows:

 public static String getnumberstr (int num) {if (num < 0) {return "; 
 } if (num = = 0) {return number1[0]; 
 int split = 10000; 
 int y = num/(split * split); 
 int w = (num/split)% split; 
 int g = num% split; 
 StringBuffer sb = new StringBuffer (); 
  billion if (Y > 0) {sb.append (getNumberStr1000 (y)); 
 Sb.append (Number2[5]); 
  //million if (W > 999) {sb.append (getNumberStr1000 (w)); 
 Sb.append (Number2[4]); 
   else {if (W > 0) {if (y!= 0) {sb.append (number2[0]); 
   } sb.append (getNumberStr1000 (w)); 
  Sb.append (Number2[4]); 
   }//below if (G > 0) {if (w!= 0) {if (G > 999) {sb.append (getNumberStr1000 (g)); 
    else {sb.append (number2[0]); 
   Sb.append (getNumberStr1000 (g)); 
   } else {if (y!= 0) {sb.append (number2[0]); 
  } sb.append (getNumberStr1000 (g)); 
} return sb.tostring (); } 

Full source code:

 /** * @Description: * * Package com.lulei.util; 
 public class Numberutil {private static string[] NUMBER1 = {"0", "one", "II", "three", "Restaurant", "WU", "Lu", "Qi", "ba", "Nine"}; 
  
 private static string[] NUMBER2 = {"0", "Pick Up", "Bai", "Qian", "million", "billion"}; /** * @param num * @return * @Author: Lulei * @Description: Converts numbers to uppercase/public static String Getnumberstr (i 
  NT num) {if (num < 0) {return ""; 
  } if (num = = 0) {return number1[0]; 
  int split = 10000; 
  int y = num/(split * split); 
  int w = (num/split)% split; 
  int g = num% split; 
  StringBuffer sb = new StringBuffer (); 
   billion if (Y > 0) {sb.append (getNumberStr1000 (y)); 
  Sb.append (Number2[5]); 
   //million if (W > 999) {sb.append (getNumberStr1000 (w)); 
  Sb.append (Number2[4]); 
    else {if (W > 0) {if (y!= 0) {sb.append (number2[0]); 
    } sb.append (getNumberStr1000 (w)); 
   Sb.append (Number2[4]); }//below if (G > 0) {iF (w!= 0) {if (G > 999) {sb.append (getNumberStr1000 (g)); 
     else {sb.append (number2[0]); 
    Sb.append (getNumberStr1000 (g)); 
    } else {if (y!= 0) {sb.append (number2[0]); 
   } sb.append (getNumberStr1000 (g)); 
 } return sb.tostring (); /** * @param num * @return * @Description: Capitalization of the following numbers/private static String getNumberStr1000 (i 
  NT num {if (num > 9999 | | | num < 0) {return ""; 
  int q = num/1000; 
  int b = (num/100)% 10; 
  int s = (NUM/10)% 10; 
  int g = num% 10; 
  StringBuffer sb = new StringBuffer (); 
   thousand if (q > 0) {sb.append (number1[q]); 
  Sb.append (Number2[3]); 
   }//Hundred if (B > 0) {sb.append (number1[b]); 
  Sb.append (number2[2]); 
   else {if (q!= 0) {sb.append (number2[0]); 
   }//10 if (S > 0) {sb.append (number1[s]); 
  Sb.append (number2[1]); } else {if (b!= 0) {Sb.appenD (Number2[0]); 
  }//if (G > 0) {sb.append (number1[g]); 
 return sb.tostring (); 
  public static void Main (string[] args) {//TODO auto-generated method Stub int i = 1; 
  SYSTEM.OUT.PRINTLN (i + "--" + numberutil.getnumberstr (i)); 
  i = 1001; 
  SYSTEM.OUT.PRINTLN (i + "--" + numberutil.getnumberstr (i)); 
  i = 100101; 
  SYSTEM.OUT.PRINTLN (i + "--" + numberutil.getnumberstr (i)); 
  i = 10100101; 
  SYSTEM.OUT.PRINTLN (i + "--" + numberutil.getnumberstr (i)); 
  i = 1234567890; 
 SYSTEM.OUT.PRINTLN (i + "--" + numberutil.getnumberstr (i)); } 
 
}

Test Run Results:

I hope this article will help you learn 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.