20 of the new travel notes of Java-convert numbers into uppercase letters

Source: Internet
Author: User
/*** Auxiliary class * is used to record characters and locations **/class charpos {char C; int Pos; Public charpos (char C, int POS) {This. C = C; this. pos = Pos ;}}
/*** Convert the number into uppercase characters **/public class cnnummaker {private string integerpart; // The integer part is private string decimalpart; // Private stack for fractional parts <charpos> stack; // stack private char [] arr for storing integer parts; // public cnnummaker (string strin) {// separate it into an integer part and a decimal part if (strin. contains (". ") {// contains the decimal point int dotindex = strin. indexof (". "); integerpart = strin. substring (0, dotindex); decimalpart = strin. substring (dotindex + 1 );} Else {// does not contain the decimal point integerpart = strin;} // initialization stack int length = integerpart. length (); stack = new stack <charpos> (charpos. class, length); arr = new char [length]; // enter the value for (INT I = 0; I <length; I ++) {char c = integerpart. charat (I); int Pos = length-I-1; if (C = '0') {If (stack. isempty () {stack. push (New charpos (C, POS);} else {charpos Top = stack. peek (); If (top. c! = '0') {stack. push (New charpos (C, POS) ;}} else {stack. push (New charpos (C, POS);} // insert a comma as the unit if (Pos % 4 = 0 & amp; pos! = 0) {arr [POS] = ',' ;}// clear the trailing zero charpos Top = stack. peek (); While (top. C = '0') {stack. pop (); If (stack. isempty () = false) {Top = stack. peek ();} else {stack. push (top); break ;}} Public String tostring () {stringbuilder sb = new stringbuilder (); // If (Long. parselong (integerpart) = 0) {sb. append ("zero");} else {While (stack. isempty () = false) {charpos poped = stack. pop (); int T1 = poped. POS/4; int t2 = T1 * 4; I F (ARR [T2]! = 0 & '0 '! = Poped. c/* & poped. pos = T2 */) {sb. insert (0, getwanunit (T2); // arr [T2] = 0;} If ('0' = poped. c) {sb. insert (0, '0');} else {sb. insert (0, getcnuppernumber (poped. c) + getunit (poped. pos) ;}}// process the fractional part if (decimalpart! = NULL & decimalpart. length ()> 0) {sb. append ("point"); For (INT I = 0; I <decimalpart. length (); I ++) {sb. append (getcnuppernumber (decimalpart. charat (I);} return sb. tostring () ;}// obtain the numbers in uppercase. Private Static char getcnuppernumber (char c) {final char [] uppers = {'0', 'yi', 'er ', 'san', 'Ta', 'wu', 'lu', 'taobao', 'hangzhou'}; string STR = string. valueof (c); int number = integer. parseint (STR); Return uppers [number];} // obtain the unit Private Static string getunit (int pos) {final string [] Units = {"", "Pick up", "Hundred", "buckets"}; int mod = POS % 4; return units [mod];} // obtain the Private Static string getwanunit (int pos) {final string [] WANs = {"," MB ", "jing", "gou", "jian", "Zheng", "Zai "}; return WANs [POS/4];} public static void main (string [] ARGs) {// cnnummaker c = new cnnummaker ("10000"); // C. tostring (); string [] arr = {"0", "1", "10", "100", "1000", "10000", "100000 ", "1000000", "10000000", "100000000", "1000000000", "10123", "100123", "1000123", "10012003", "1234567890.043243", "567891234567890.043243 ", "3243243344567890.043243", "24", "108", "9234", "9087", "9008", "10001", "100001", "10000001", "1000000001 ", "10000000001", "100000000001", "1000000000001", "10000000000001", "10007600089001"}; For (string STR: ARR) {try {cnnummaker c = new cnnummaker (STR ); system. out. println (STR + "->" + C. tostring ();} catch (exception e) {e. printstacktrace (); system. out. println ("error:" + Str );}}}}

Output:

0-> 0 1-> 1 10-> 1 million-> 1 million-> 1 million 100-> 1 million 1000-> 1 million 10000-> 1 million 100000000 million-> 1000000000 million-> 10123 million-> 100123 million-> 1000123 million, 10012003 million-> million, million-> million -> 1234567890.043243 million yuan, 567891234567890.043243 yuan, and Yuan-> yuan, Yuan, Yuan, and Yuan hundreds of megabytes of logs, hundreds of millions of logs, tens of thousands of logs, tens of thousands of logs, hundreds of millions of logs, and hundreds of thousands of logs. hundreds of megabits, hundreds of megabytes, hundreds of millions, tens of billions, hundreds of millions, tens of thousands, hundreds of thousands, and hundreds of thousands 108-> 9234-> 9087-> 9008-> 10001-> 100001-> 10000001-> -> yi 1000000001, Yi 10000000001-> yi 100000000001-> yi 10, Yi 10, Yi 1000000000001-> yi 1, Yi 1, Yi 10000000000001-> yi 1, Yi 1, Yi 1 0g and 0g

 

20 of the new travel notes of Java-convert numbers into uppercase letters

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.