1. program description: converts a floating point number to the reading string of the coin.
2. Procedures and precautions
Process:
* Receives a double value from the user.
* Process the double value to obtain the integer part and the decimal part.
* Process integer and decimal parts respectively.
* Merge integer and decimal string results, output results, and end.
Note:
Rounding by math. Round ()
Use string. tochar () to convert a String Array
Mainly solve the problem
Zero processing in a string, which is divided into two situations
(1) Whether the first priority is 0
Not processed. Generally, the first part of the input value is not 0.
(2) 0 at the end
Directly without translation
(3) processing of a single 0 in the middle and a continuous 0
Each time we process four digits, we divide the first four digits into the other four digits and the last four digits.
A. first 4 digits: 0 in the middle is translated into zero; 0 in a row is translated into zero, and 0 at the end does not need to be translated.
? 100. Continuous 0 to the end should not be translated.
B. The other four digits: the current situation should be the same as.
C. Processing with all 0 values
Translation 0 RMB
(5) When the integer and decimal part are both 0
(4) how to deal with a large number of digits
Currently, 12 bits are processed by default.
(5) divide the number into four digits based on different units for processing.
1-4 yuan
5-80 thousand
9-1.2 billion
* Code
Import Java. util. producer; import COM. sun. CORBA. se. SPI. extension. zeroportpolicy; import COM. sun. XML. internal. WS. API. streaming. xmlstreamreaderfactory. zephyr; public class num2rmb {private double digitalvalue; private Boolean zero; string [] strhan = {"zero", "one", "two", "three", "Si ", "Wu", "Lu", "Jun"}; string [] moneycount = {"", "Pick", "Jun ", "metadata"}; string [] moneyunit = {"Yuan", "", ""}; // The default constructernum2rmb () {Digit Alvalue = 0.0; zero = false;} // the definite constructed with the @ paramnum2rmb (double digitalrmbvalue, Boolean flag) {digitalvalue = digitalrmbvalue; zero = flag ;} // divide the digital value into the integer part and decimal part // return the array of string [] // string [0] is the integer part // string [1] is decimal partprivate string [] divide (double digitalrmbvalue) {// get the integer part of the digi Tal moneylong intpart = (long) (digitalrmbvalue); // get the decimal part of the digital moneylong decimalpart = math. round (digitalrmbvalue-intpart) * 100); // another method use integer. tostring (int A) // If the decimal part is equal to 0, then we will give 0 to the stringstring strintpart = ""; string strdecimalpart = ""; if (intpart = 0) strintpart = "0"; else {strintpart = "" + intpart;} If (decimalpart <10 & Decimalpart> = 0) strdecimalpart = "0" + decimalpart; else {strdecimalpart = "" + decimalpart;} If (intpart = 0 & decimalpart = 0) {zero = true;} return New String [] {strintpart, strdecimalpart};} // process the basic unit string with at most 12 charactersprivate string prodecimalrmb (string Str) {string strresult; char [] strrmb = new char [20]; strrmb = Str. tochararray (); If (strrmb [0] = '0' & strrmb [1] = '0') {strresult = "";} else {int M = strrmb [0]-'0'; int n = strrmb [1]-'0 '; strresult = strhan [m] + "" + strhan [N] + "points";} return strresult;} // process the 4 first bit number as the form: {& quot; 456 & quot;} // The first kind situationprivate string profirstfourbit (string Str) {char [] firstfourbitrmb = new char [20]; firstfourbitrmb = Str. tochararray (); int Len = firstfourbitrmb. length; string strresult = ""; for (INT I = 0; I <Len; I ++) {// The first zero // if the last bit is 0, we process ITIF (firstfourbitrmb [I] = '0' & I <Len) {Boolean zeroflag = false; // This WHILE LOOP expression may have an error and the pointer is out of bounds for access, note the order of expression statements while (I <Len & firstfourbitrmb [I] = '0') {zeroflag = true; I ++;} if (I! = Len & zeroflag = true) strresult + = "zero"; I --;} // If non-zero bit else {int M = firstfourbitrmb [I]-'0'; strresult + = strhan [m] + moneycount [len-i-1]; // The following }}// end for return strresult;} // profirstfourbit () // process the middle or the last four bit situation/* Private string prosecondfourbit (string Str) {// int I = 0; string strresult = ""; char [] secondfourbitrmb = new char [20]; secondfourbitrmb = Str. tochararray (); int Len = secondfourbitrmb. length; for (I = 0; I <secondfourbitrmb. length; I ++) {If (secondfourbitrmb [I] = '0') {Boolean zeroflag = false; while (secondfourbitrmb [I] = '0' & I <Len) {If (zeroflag = false) {zeroflag = true; strresult + = "zero ";} I ++;} I --;} // If non-zero bit else {int M = secondfourbitrmb [I]-'0 '; strresult + = strhan [m] + moneycount [len-i-1]; // The following }}// end for return strresult;} // prosecondfourbit () */public static void main (string [] ARGs) {string strindicator = "begin to test the digital RMB"; system. out. println (strindicator); system. out. println ("Please input the Digital Money:"); string strtestrmb = "2356.782"; string [] strtemprmb = new string [2]; double strvalue = 0d; using SC = new using (system. in); strvalue = SC. nextdouble (); num2rmb Nr = new num2rmb (); // divide the digital RMB strtemprmb = Nr. divide (strvalue); // process the decimal ditial money string result1 = Nr. prodecimalrmb (strtemprmb [1]); // process the interger part money string result2 = ""; int Len = strtemprmb [0]. length (); If (LEN> 12) system. out. println ("the data you input is too large to translate"); else {If (LEN> = 9) {string strbit1 = strtemprmb [0]. substring (0, len-8); string strbit2 = strtemprmb [0]. substring (len-8, len-4); string strbit3 = strtemprmb [0]. substring (len-4, Len); string strresult1 = Nr. profirstfourbit (strbit1) + ""; string strresult2 = Nr. profirstfourbit (strbit2) + "Ten Thousand"; string strresult3 = Nr. profirstfourbit (strbit3) + ""; result2 = strresult1 + strresult2 + strresult3;} else if (LEN> = 5) {string strbit1 = strtemprmb [0]. substring (0, len-4); string strbit2 = strtemprmb [0]. substring (len-4, Len); string strresult1 = Nr. profirstfourbit (strbit1) + "Ten Thousand"; string strresult2 = Nr. profirstfourbit (strbit2) + ""; result2 = strresult1 + strresult2;} else if (LEN> = 1) {If (Nr. zero = true) {result2 = "0 RMB";} else result2 = Nr. profirstfourbit (strtemprmb [0]) + "Yuan";} system. out. println ("translate the form of the money is:"); system. out. println (result2 + result1) ;}// main ()}
// This while loop expression may be incorrectly accessed because the pointer is out of bounds. Pay attention to the order of expression statements.
While (firstfourbitrmb [I] = '0' & I <Len)