Java Floating Point-to-RMB reading method and java reading Method

Source: Internet
Author: User

Java Floating Point-to-RMB reading method and java reading Method

Conversion of Java floating point to RMB reading

 

  • The number of the [integer part] starts from a single digit. The four digits are divided into one group. If the highest digit does not have a number, 0 is added, and the total number of arrays is recorded. There are three groups in total, it is also a 3*4 two-dimensional array;
  • For unified processing of each array, the "zero" at the beginning will be removed, and the "zero" before the words "million" and "Ten Thousand" and "RMB" will be removed;
  • When 0 is read, check whether the previous digit is 0. If the previous digit is 0, no processing is performed. If the previous digit is not 0 or the previous digit does not exist, write down "zero ";
  • Read each number, convert it to the corresponding upper-case Chinese character, and add the weight "marker, marker, and pickup". According to the number of arrays read, add "million" and "million" Yuan later ";
  • The decimal part is processed as a one-dimensional array. Only two decimal places are retained, corresponding to the "angle" and "points". The number 0 is not processed;

 

The test number is 120,300, 7600.89.
Package theTest; public class NumToRmb {private String [] hanArr = {"zero", "one", "two", "three", "Si", "Wu ", "land", "Shanghai"}; private String [] unitArr = {"Shanghai", "Shanghai", "Pick "}; /*** split a floating point number into an integer part and a decimal part string * @ param num the floating point number to be decomposed * @ return the integer part and the decimal part, the first array element is the integer part, and the second array element is the decimal part. */Private String [] divide (double num) {// forcibly convert a floating point number to the long type, that is, obtain the integer part of it long zheng = (long) num; // The floating point minus the integer part to get the fractional part. Multiply the fractional part by 100 to get the two decimal places long xiao = Math. round (num-zheng) * 100); // here two methods are used to convert an integer to a String return new String [] {String. valueOf (zheng), xiao + ""};}/*** divide integers into four numbers as a group of strings, convert them into uppercase and lowercase numbers, and then connect them, get the final RMB reading Method * @ param num the floating point number to be converted to an upper-case number * @ return returns the RMB reading method of the entire number */private String conHanStr (double num) {// store integer and decimal result S Tring resZheng = ""; String resXiao = ""; // stores the final result String result = ""; // stores the integer and fractional strings respectively. String zhengStr = divide (num) [0]; String xiaoStr = divide (num) [1];/* processing of fractional digits */if (xiaoStr. length () = 1) {xiaoStr = "0" + xiaoStr;} resXiao = xiao2Han (xiaoStr);/* integer Processing * // number of record strings, is the nth array int countArr = 0; String tempStr = ""; // number of record digits int countNum = 0; for (int I = zhengStr. length ()-1; I> = 0; I --) {countNum ++; tempStr = zhengStr. charAt (I) + TempStr; if (I-1 <0) {// I is already the last number, not enough bits, fill 0 while (tempStr. length () <4) {tempStr = "0" + tempStr;} countArr ++; resZheng = zheng2Han (countArr, tempStr) + "" + resZheng ;} else {if (countNum = 4) {countArr ++; resZheng = zheng2Han (countArr, tempStr) + "" + resZheng; countNum = 0; tempStr = "";}}} // remove the zero if (resZheng. charAt (0) = '0') {resZheng = resZheng. substring (1, resZheng. length ();}/* connections between integers and decimals * // indicates that the integer part is 0 if (resZheng. ch ArAt (0) = 'meta') {resZheng = "zero" + resZheng;} result = resZheng + resXiao; return result ;} /*** each four-digit integer group is converted to an upper-case number * @ param count the number of the array (1-3) * @ param str the array to be converted * @ return returns the renminbi reading method for this array */private String zheng2Han (int count, String str) {String result = ""; for (int j = 0; j <4; j ++) {int tempNum = str. charAt (j)-48; if (tempNum! = 0) {if (j! = 3) {result + = hanArr [tempNum] + unitArr [j];} else {result + = hanArr [tempNum];} else {if (J-1 <0) | (str. charAt (J-1)-48! = 0) {result + = hanArr [tempNum] ;}}// remove the zero if (result. charAt (result. length ()-1) = '0') {result = result. substring (0, result. length ()-1);} // Add ", RMB, angle, and minute" after Zero removal ". Switch (count) {case 1: result + = ""; break; case 2: result + = ""; break; case 3: result + = "; break; default: System. out. println ("only supports numbers not greater than 999999, 9999, 9999.99! "); Return" ";} return result ;} /*** convert decimal to uppercase digits * @ param str array to be converted * @ return returns the decimal reading Method */private String xiao2Han (String str) {String result = ""; if (! Str. equals ("00") {for (int I = 0; I <2; I ++) {int tempNum = str. charAt (I)-48; if (tempNum! = 0) & (I = 0) {result + = hanArr [tempNum] + "";} if (tempNum! = 0) & (I = 1) {result + = hanArr [tempNum] + "points" ;}} return result ;} /*** main function * @ param args */public static void main (String [] args) {NumToRmb nr = new NumToRmb (); System. out. println (nr. conHanStr (double) 12030060078.95 ));}}

 

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.