Convert a number to the capitalization of the Chinese amount (with detailed steps to solve the problem)

Source: Internet
Author: User

1 /*2 * Program Purpose: Enter a number from the command line and convert it to uppercase in Chinese amount3 * Ideas:4 * Required objects: Use two arrays, a number in Chinese capital, a deposit amount unit;5 * In order to maintain the accuracy of the relative accuracy, the use of BigDecimal class; (Friends who do not know, the Internet a search will know);6 * I use the StringBuilder class to save the converted results, (in fact, with String,stringbuffer can be)7 * Procedure: Enter a number of type Double ——— > Convert to BigDecimal class ——— > Rounding to a number of long types;8 * First judge the special situation of the fractional part;9 * uniform processing of numbers; (one per "%10 method") to populate the data in a way that corresponds to a pre-set array)Ten * Output the final result; One * Difficult point: the treatment of zero; A  *  - * 1, Arabic numerals in the middle of "0", the Chinese capital to write "0" word, such as ¥1409.50 should be written in the Yuan Qian Shangyuan Wu Kok; - 2, Arabic numerals in the middle of a number of "0", the amount of Chinese capital can only write a "0" word, such as ¥6007.14 should be written in RMB thousand 0 qi yuan cents.  the 3, the Arab amount of digital Wanwi yuan is "0", or a number in the middle of continuous several "0", million bits, the yuan is also "0" but thousands, the corner is not "0", the Chinese capital amount can only write a 0 words (I use this here), can also not write "0" word, such as ¥1680.32 should be Written in RMB Ba 0 three cents, or write a Yuan Qian ba three cents. ". And if the ¥107000.53 should be written in the yuan of the Million QI thousand yuan 0 cents, or write a Chinese yuan to pick up the zero qi thousand yuan Wooker points.  - 4, the Arab amount of digital corner is "0" and the sub-position is not "0", the Chinese capital amount "Yuan" should be written after "0" word, such as ¥16409.02 should be written in RMB a Wan Shangyuan 0 two points, and if ¥325.04 should be written in RMB triple two pick dollar 0 points.  -  *  - * Method: Deal with the situation; + * Note: In fact, can be more concise write, but in order to see more clearly, more good programming habits, so some write "wordy", please forgive me!  - * If you find that my procedures are inappropriate, but also look at the comments, study together; +  */ A  at ImportJava.math.BigDecimal; - ImportJava.util.Scanner; -  -  Public classNUMTOCN { -      -     //the use of string here, StringBuffer can actually be, but from the variable length and single-threaded aspects of consideration, StringBuilder more appropriate; in     Private StaticStringBuilder SB =NewStringBuilder (); -      to     Private Static FinalString[] Cn_number = {"0", "one", "II", "three", "the", "WU", "Lu", "Qi", "ba", "JIU"}; +     Private Static FinalString[] Cn_unit = {"Min", "angle", "circle", "Pick", "Bai", "Thousand", "Million", "Pick", "Bai", "Qian", "billion", "pick", "Bai", "thousand", "trillion", "pick", "Bai", "Qian", "Shun" }; -  the     //some extra characters to add *     Private Static FinalString cn_negative = "negative"; $     Private Static FinalString cn_full = "integer";Panax Notoginseng     Private Static FinalString Cn_zero_full = "0 Rounding"; -     Private Static Final intPercision = 2;//accuracy the      +      Public StaticString numtocn (BigDecimal numofmoney) { A          the         //returns 1, 0, or 1 when the value of this BigDecimal is negative, 0, or positive.  +         intSignum =numofmoney.signum (); -          $         //if the input is 0, the output 0 is rounded; $         if(Signum = = 0) { -             returnCn_zero_full; -         } the          -         //Rounding the amount to a long integer, moving the decimal point to the right by two bits, then rounding, taking the absolute value, and finally converting it to a long integer;Wuyi         LongNumber = Numofmoney.movepointright (percision). Setscale (0, BIGDECIMAL.ROUND_HALF_UP). ABS (). Longvalue (); the         intNumindex = 0;//Number of recorded numbers; -         BooleanGetzero =false; Wu /* - * Idea: To judge the specific situation of the fractional part first, the fundamental reason is that the fractional and integer parts are slightly different in dealing with the "0" problem, and avoid the situation shown in 1 ; About  */ $         //get the fractional part (two digits after the decimal point); -         LongScale = number% 100; -         if(Scale = = 0) {//if the number of parts is divided into "00" when the situation; Sao year, don't forget to append special characters at the end: whole -Numindex + = 2; AGetzero =true; +Number/= 100;//removed from number is 0; the sb.append (cn_full); -}Else if(scale% 10 = = 0) {//if the number of parts is divided into "*0" when the situation; $Numindex + = 1; theGetzero =true; theNumber/= 10;//removed from number is 0; the         } the          -         //excluding the two fractional parts of the special case, the decimal and integer processing is the same as the same!  in          while(true) { the             //cycle end condition; the             if(Number <= 0){ About                  Break; the             } the              the             //each time by taking the remainder to the last digit; +             intNumunit = (int) (number% 10); -             if(Numunit! = 0){ theSb.insert (0, Cn_unit[numindex]);//Add units FirstBayiSb.insert (0, Cn_number[numunit]);//The Chinese expression in the corresponding array is added according to the numeric value; theGetzero =false;//indicates that the current number is not 0; the             } -             Else { -                 //It means it's last number is not 0, then print out 0; the                 if(!Getzero) { theSb.insert (0, Cn_number[numunit]); the                 } the                 //If the angular division is zero, then print 0; -                 if(Numindex = = 2) { the                      if(Number > 0) { theSb.insert (0, Cn_unit[numindex]); the                      }94}Else if((numIndex-2)% 4 = = 0 && number% 1000!=0) {//The first condition is to print "round, million, million" for every 4 digits, and the second condition is to avoid the occurrence of 3; theSb.insert (0, Cn_unit[numindex]); the                 } theGetzero =true;//set it to true, then if the next one is still 0, it will not print again ' 0 '; avoid the case of Figure 2;98             } About              -          //remove the last number each time101Number = NUMBER/10;102numindex++;103         }104          the         //if Signum = =-1, the input number is negative, appending the special character to the front: negative106         if(Signum = =-1) {107Sb.insert (0, cn_negative);108         }109          the         returnsb.tostring ();111     } the     113      the      Public Static voidMain (string[] args) { theScanner scan =NewScanner (system.in); the         DoubleMoney =scan.nextdouble ();117BigDecimal Numofmoney =NewBigDecimal (money);118String s =NUMTOCN.NUMTOCN (Numofmoney);119 System.out.println (s); -     }121 122}

Figure One:

Figure II:

Figure three: The top number is 100000000, because it is used in the test, so it will be shown in the figure, do not tangle;

Convert a number to the capitalization of the Chinese amount (with detailed steps to solve the problem)

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.