Conversion of Java floating point to RMB reading

Source: Internet
Author: User

Conversion of Java floating point to RMB reading

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;
  • Each array is processed in a unified manner. When 0 is read, check whether the previous bit is 0. If the previous bit is 0, no processing is performed, if the last 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 the corresponding "million", "", and "RMB ";
  • Finally, remove the "zero" at the beginning, and remove the "zero" before the words "", "", and "RMB;
  • 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", "Lu", "Lu ", "identifier", "identifier "};

Private String [] unitArr = {"Hangzhou", "Shanghai", "Pick "};

/**
* Splits a floating point number into integer and decimal strings.
* @ Param num refers to the floating point number to be decomposed.
* @ Return refers to the integer part and decimal part. The 1st array elements are integers and the 2nd array elements are decimal parts.
*/
Private String [] divide (double num ){
// Forcibly convert a floating point to the long type to obtain its integer
Long zheng = (long) num;

// The floating point minus the integer part to get the fractional part. Multiply the fractional part by 100 to get two decimal places.
Long xiao = Math. round (num-zheng) * 100 );

// Here two methods are used to convert integers into strings
Return new String [] {String. valueOf (zheng), xiao + ""};
}
/**
* Divide integers into four numbers into a group of strings, convert them into uppercase numbers, and connect them to obtain the final RMB reading method.
* @ Param num refers to the floating point number to be converted to an upper-case number.
* @ Return returns the renminbi reading method of the entire number.
*/
Private String conHanStr (double num ){

// Store integer and decimal results
String resZheng = "";
String resXiao = "";

// Store the final result
String result = "";

// Stores integer and decimal strings respectively.
String zhengStr = divide (num) [0];
String xiaoStr = divide (num) [1];

/* Processing the fractional part */
If (xiaoStr. length () = 1 ){
XiaoStr = "0" + xiaoStr;
}

ResXiao = xiao2Han (xiaoStr );

/* Integer Processing */
// Record the number of strings, which is the nth Array
Int countArr = 0;
String tempStr = "";

// Number of records
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. It is not enough. Fill in 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 starting zero
If (resZheng. charAt (0) = '0 '){
ResZheng = resZheng. substring (1, resZheng. length ());
}

/* Connections between integers and decimals */
// Indicates that the integer is 0.
If (resZheng. charAt (0) = 'meta '){
ResZheng = "zero" + resZheng;
}

Result = resZheng + resXiao;

Return result;
}

/**
* Each 4-digit integer group is converted to an upper-case number.
* @ Param count the number of records (1-3)
* @ Param str the array to be converted
* @ Return returns the RMB reading method of the 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 end zero
If (result. charAt (result. length ()-1) = '0 '){
Result = result. substring (0, result. length ()-1 );
}

// Add "hundreds of millions, tens of thousands, Yuan, angle, and points" after Zero removal ".
Switch (count ){
Case 1:
Result + = "RMB ";
Break;
Case 2:
Result + = "10 thousand ";
Break;
Case 3:
Result + = "";
Break;
Default:
System. out. println ("only supports numbers not greater than 999999, 9999, 9999.99! ");
Return "";
}
Return result;
}

/**
* Converts decimals to uppercase digits.
* @ Param str the 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] + "Minute ";
}
}
}
Return result;
}

/**
* Main Function
* @ Param args
*/
Public static void main (String [] args ){
NumToRmb nr = new NumToRmb ();
System. out. println (nr. conHanStr (double) 12030060078.95 ));
}
}

This article permanently updates link: https://www.bkjia.com/Linux/2018-03/151275.htm

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.