Java converts an integer to a Chinese capital amount.

Source: Internet
Author: User

Java converts an integer to a Chinese capital amount.

In daily life, we often convert Arabic numerals into uppercase letters: "0", "1", "2", "3", "Si ", "Wu", "Lu", "Jun", "Pick", "Jun", "Jun ", therefore, you can write a class to convert the function.
Implementation process:
To solve this problem, we first split the number by four digits (in line with the habit of reading the number) and then read the four digits. 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) % 10; int s = (num/10) % 10; int g = num % 10; StringBuffer sb = new StringBuffer (); // thousands of if (q> 0) {sb. append (NUMBER1 [q]); sb. append (NUMBER2 [3]);} // hundreds of 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]) ;}// if (g> 0) {sb. append (NUMBER1 [g]);} return sb. toString ();}

Below we only need to write a method to implement each four-digit reading method and the corresponding unit. 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 (); // if (y> 0) {sb. append (getNumberStr1000 (y); sb. append (NUMBER2 [5]);} // 999 if (w>) {sb. append (getNumberStr1000 (w); sb. append (NUMBER2 [4]);} e Lse {if (w> 0) {if (y! = 0) {sb. append (NUMBER2 [0]);} sb. append (getNumberStr1000 (w); sb. append (NUMBER2 [4]) ;}// 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 ();}

Complete source code:

/*** @ Description: */package com. lulei. util; public class NumberUtil {private static String [] NUMBER1 = {"zero", "one", "two", "three", "Si", "Wu ", "land", "login"}; private static String [] NUMBER2 = {"zero", "pick up", "unlock ", "classification", "", ""};/***** @ param num * @ return * @ Author: lulei * @ Description: convert the number to uppercase */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 (); // if (y> 0) {sb. append (getNumberStr1000 (y); sb. append (NUMBER2 [5]);} // 999 if (w>) {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]) ;}// 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: converts the case sensitivity of numbers below */private static String getNumberStr1000 (int 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 (); // thousands of if (q> 0) {sb. append (NU MBER1 [q]); sb. append (NUMBER2 [3]);} // hundreds of 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]) ;}// 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 result:

I hope this article will help you learn java programming.

Articles you may be interested in:
  • Java obtains the absolute value of an integer without any judgment or comparison statements, including
  • Example of converting a java INTEGER (in seconds) to a time/minute/second format
  • Java converts numbers to uppercase
  • How to convert IP addresses and integers in JAVA
  • How to convert the numeric capital amount in JAVA

Related Article

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.