Java lower-case amount conversion upper-case amount

Source: Internet
Author: User

Import Java. io. *; <br/> Import Java. lang. string; <br/> public class switchover {<br/> Public static string [] strchi = {"zero", "one", "two", "three ", "Si", "Wu", "Lu", "Jun" };< br/> Public static string [] strunits = {"", "", "Pick", "weight", "weight", "", "weight", "million", "weight ", "Hour", "Hour", "MB", "pick up", "Hour", "Minute "}; <br/> // verify that the entered number meets the requirements <br/> Public Boolean isnumeric (string Str) {<br/> // If (Str. matches ("// D + (. // D +) $ ") {<br/> If (Str. matches ("[ // D.] + ") {<br/> If (Str. substring (0, 1). Equals (" 0 ")&&! (Str. substring (1, 2 ). equals (". ") {<br/> return false; <br/>}else {<br/> return true; <br/>}< br/>}else {<br/> return false; <br/>}</P> <p> // converts lowercase letters to uppercase letters. <br/> Public String numtochi (string STR, int iflag) {<br/> string ireturn = strchi [integer. parseint (STR)] + strunits [iflag]; <br/> If (Str. equals ("0") & (iflag = 5) {return "Ten Thousand" ;}< br/> If (Str. equals ("0") & (iflag = 9) {return "" ;}< br/> If (Str. equals (" 0 ") & (iflag = 13) {return" MB ";}< br/> If (Str. Equals (" 0 ") & (iflag! = 5) | (Str. equals ("0") & (iflag = 9) | (Str. equals ("0") & (iflag = 13) {<br/> return "0"; <br/>} else {<br/> return ireturn; <br/>}</P> <p> // remove unnecessary zeros <br/> Public String delzero (string Str) {<br/> for (INT I = 0; I <Str. length ()-1; I ++) {<br/> If (Str. indexof ("zero")> 0) {STR = Str. replace ("zero", "zero") ;}< br/> If (Str. indexof ("0 thousand")> 0) {STR = Str. replace ("0 thousand", "") ;}< br/> If (Str. indexof ("0 billion")> 0) {STR = Str. replace ("0 billion", "") ;}< br/> If (Str. indexof ("0 MB")> 0) {STR = Str. replace ("0 MB", "MB") ;}< br/> If (Str. indexof ("")> 0) {STR = Str. replace ("circle", "circle") ;}< br/> If (Str. substring (0, 1 ). equals ("0") {STR = Str. replace (Str. substring (0, 1), ""); // remove the first zero <br/>}< br/> return STR; <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> switchover = new switchover (); <br/> string inwrite = "0"; // read the input number <br /> Boolean flag = false; // do {} the flag used by while (FLAG) <br/> try {<br/> do {<br/> system. out. print ("enter a number ('/' to exit):"); <br/> do {<br/> bufferedreader BR = new bufferedreader (New inputstreamreader (system. in); <br/> inwrite = BR. readline (); <br/> // exit <br/> If (inwrite. equals ("/") {system. out. println ("exited! "); Return ;}< br/> // Digital Detection <br/> If (inwrite. indexof (". ")> 0) {If (inwrite. indexof (". ")> 16) {system. out. print ("the number of digits is too long. Enter again:"); flag = true;} else {flag = false;} else {If (inwrite. length ()> 16) {system. out. print ("the number of digits is too long. Enter again:"); flag = true;} else {flag = false ;}< br/> If (! Switchover. isnumeric (inwrite) {system. out. print ("incorrect input. Please enter again:"); flag = true;} else {flag = false ;}< br/>} while (FLAG ); </P> <p> string ivalue = ""; // record the final output result </P> <p> // if there is a decimal point, <br/> If (inwrite. indexof (". ")> 0) {<br/> for (INT I = 0; I <inwrite. indexof (". "); I ++) {<br/> ivalue + = switchover. numtochi (inwrite. substring (I, I + 1), inwrite. indexof (". ")-I) ;}// extract the number before the decimal point </P> <p> If (! Inwrite. substring (0, inwrite. indexof (". ")). equals ("0") {ivalue + = "circle";} // Add a circle with a decimal point <br/> int I = (inwrite. substring (inwrite. indexof (". "))). length (); // calculate the number of digits after the decimal point <br/> if (I> 3) {I = 3; // if the number after the decimal point is greater than two digits, only two valid decimal places are taken. <br/>} else {I = (inwrite. substring (inwrite. indexof (". "))). length () ;}< br/> int iflag = 16; // array subscript of the unit angle and Points <br/> for (int s = inwrite. indexof (". ") + 1; S <inwrite. indexof (". ") + I; s ++) {iflag ++; ivalue + = switchover. numtochi (inwrite. substring (S, S + 1), iflag );} </P> <p> // If the decimal point does not exist <br/>} else {<br/> for (INT I = 0; I <inwrite. length (); I ++) {ivalue + = switchover. numtochi (inwrite. substring (I, I + 1), inwrite. length ()-I) ;}< br/> ivalue = ivalue + "round" ;}< br/> system. out. println ("output result:" + switchover. delzero (ivalue); <br/>}while (true); <br/>}catch (ioexception e) {<br/> system. out. println (E ); <br/>}</P> <p> ------------------------------------------------------ <br/> enter a number ('/' to exit ): 0.1 <br/> output result: corner 1 <br/> enter a number ('/' exit): 1.1 <br/> output result: yiyuan yijiao <br/> enter a number ('/' exit): 10.1 <br/> output result: <br/> enter a number ('/' Exit): 10000.01 <br/> output result: 1010.01 <br/> enter a number ('/' to exit): <br/> output result: 1. 0. 0. 1. 0. <br/> enter a number ('/' to exit): 100001.11 <br/> output result: 10101010.10 yuan and 1 yuan <br/> enter a number ('/' exit): <br/> output result: 0 <br/> enter a number ('/' to exit): 9080070650400302.01 <br/> output result: please enter a number ('/' to exit): <br/> quit ----------------------------------------------------------

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.