Conversion of java RMB reading method and java RMB reading Method
Converts a floating point to a Renminbi reading string.
Write a program to convert a floating point number into a reading string of RMB.
Question Analysis:
The floating point number is divided into integer and decimal parts, and the integer and decimal parts are processed separately.
The processing of decimal parts is relatively simple. You just need to extract the reserved double-digit characters and convert them into several character strings.
The integer part can be divided into several arrays with four digits, each of which is thousands, hundreds, and dozens of digits (add an appropriate unit ).
If the four digits appear in 1 ~ 4 bits, then add 'meta'. If it appears in 5 ~ The value range is 8 ~ If the value is 12 bits, the value is '100 '.
If the value exceeds 12 digits, the double storage overflow may cause data errors.
Code compilation analysis:
To divide a floating point number into integer and decimal digits, you only need to forcibly convert a double floating point to a long integer,
After the floating point of the double type is subtracted from the integer of the long type, the fractional part of the floating point is obtained. Because only two digits are required, the value is multiplied by 100.
The Math. round function is used to integer the fractional part. Use "zheng +" or "String. valueOf (xiao)" to convert an integer to a String.
The decimal part of the string is judged one by one, followed by an appropriate addition of angle, points, you can convert successfully.
Set the length of the integer string to a multiple of 4 to group the four digits. Use the string truncation function to intercept the last 4
And update the original string to the string after the last four digits.
A four-character string is used to convert Chinese characters. If the number is not the last digit and the number is not zero, the Unit must be added.
Note the sequence when connecting the four converted Chinese character strings.
Special case analysis:
If the first digit after the decimal point is 0, for example, 19.08, the length of the decimal part of the string needs to be considered during decimal part conversion. If the length is 1, it is specially processed.
If the number is shown in 10009 and the last four digits are truncated to 0009, an error may occur during conversion. You need to convert the number to 9 and then convert it to a string of 9 characters.
Conversion.
If the number is shown in 1009 and the number is zero adjacent to each other, You need to determine whether to convert the previous character when the single character is 0.
If the number is shown in 1890 and the last digit is zero, you need to determine whether the converted string is zero or not.
If the number is 0.89, or 10000.89, and the 4-digit string is 0, the conversion operation must be performed separately.
1 package RMB reading method; 2 3 import java. util. arrays; 4 import java. util. token; 5 6 // write a program to convert a floating point number into a Renminbi reading string. For example, to convert 1006.333 to a shard of Yuan,/D: \ Crazy Java handout \ codes \ 04 \ 4.6 NUM2RMB 8 9 public class RMB {10 private String [] str1 = {"zero", "one", "two ", "San", "Si", "Wu", "Lu", "Lu", "Wu", "Wu "}; 11 private String [] str2 = {"Ten", "Hundred", "thousands"}; 12 private String [] str3 = {"", "points "}; 13 private String [] str4 = {"Yuan", "", ""}; 14 // Splits a floating point number into integer characters String 15 private String divadeZheng (double num) 16 {17 long zheng = (long) num; 18 // integer to 19 return zheng + ""; 20} 21 // split the floating point number into a decimal String 22 private String divadeXiao (double num) 23 {24 long zheng = (long) num; 25 // obtain the decimal part of the Floating Point Number 26 long xiao = Math. round (num-zheng) * 100); 27 // convert an integer to a String 28 return String. valueOf (xiao); 29} 30 31 // convert the numeric String in the decimal part into a Chinese character String 32 private String toStr1 (String xiao) 33 {34 String re1 = ""; 35 int len = xiao. length (); 36 if (len = 2) 37 {38 for (int I = 0; I <2; I ++) 39 {40 // convert the string to a number 41 int num = xiao. charAt (I)-48; 42 // if the number is not zero, the unit needs to be added. if the number is zero, 43 if (num! = 0) 44 {45 re1 + = str1 [num] + str3 [I]; 46} 47} 48} 49 // If the length is 1, for example: 12.09 decimal part is 9 50 if (len = 1 & xiao. charAt (0 )! = '0') 51 {52 int num = xiao. charAt (0)-48; 53 // at this time, only the score is added. The unit is str3 [1] 54 re1 + = str1 [num] + str3 [1]; 55} 56 return re1; 57} 58 59 // convert a four-digit numeric String into a Chinese character String 60 private String toStr (String zheng) 61 {62 String re = ""; 63 int j = 0; 64 int lenz = zheng. length (); 65 // in special cases, for example, when the input is 0.89, the integer string 0 is processed 66 if (zheng. charAt (0) = '0' & lenz = 1) 67 {68 re + = "0"; 69 return re; 70} 71 // traverse each digit 72 for (int I = 0; I <lenz; I + +) 73 {74 int num = zheng. charAt (I)-48; 75 // if it is not the last digit and the number is not zero, you need to add the unit 76 if (I! = Lenz-1 & num! = 0) 77 {78 re + = str1 [num] + str2 [lenz-2-i]; 79} 80 // special treatment for continuous 0 appearance, such as 1009, the second zero does not operate 81 else if (! (Num = 0 & re. charAt (J-1) = '0') 82 {83 re + = str1 [num]; 84} 85 j = re. length (); 86} 87 // In special cases, the last bit is zero, for example, 1990. Delete the zero 88 if (re. charAt (J-1) = '0') 89 {90 re = re. substring (0, J-1); 91} 92 return re; 93} 94 // processing the integer part of the amount within 12 digits, with 4 digits as a group, 95 // For example, 1234 (5678 million), 1234 (million), and (RMB ). 89, 96 private String zhengDivade (String zheng) 97 {98 int len = zheng. length (); 99 String Re = ""; 100 int n = 0; 101 // judge the integer part The length of the 102 if (len> = 9) 103 n = 3; 104 else if (len> = 5) 105 n = 2; 106 else if (len> = 1) 107 n = 1; 108 // if the number of digits is not a multiple of 4, add 0 in front, until the number of digits is a multiple of 4 109 // For example, 12, 2345 is supplemented as, 2345110 if (len % 4! = 0) 111 {112 String zh = ""; 113 int s = len % 4; 114 // Add 0115 for (int k = 0; k <(4-s ); k ++) 116 {117 zh + = "0"; 118} 119 zh + = zheng; 120 zheng = zh; 121} 122 // concatenate n combinations 123 // perform n cycles 124 for (int I = 0; I <n; I ++) 125 {126 String Zheng = ""; 127 String sss = ""; 128 // Length changes at 129 len = zheng. length (); 130 // extract the last four digits of the integer and convert them separately. 131 Zheng = zheng. substring (len-4); 132 // remove a string other than the extracted string, string updated 133 zheng = zheng. substring (0, len-4); 134 int m = 0, me = 0; 135 // convert the string to an integer, such as 0012 to 12136 for (int j = 3; j> = 0; j --) 137 {138 m = (int) (Zheng. charAt (j)-48) * Math. pow (10, 3-j); 139 me + = m; 140} 141 // Replace number 12 with a String of 12142 Zheng = String. valueOf (me); 143 // replace string 12 with 144 sss = this. toStr (Zheng); 145 // connects the units such as the upper dollar, and adds the converted Chinese character string, note that the order between each string is 146 sss + = str4 [I] + Re; 147 Re = sss; 148} 149 return Re; 150} 151 152 // master method, enter the amount, and connect the integer and fractional parts to the 153 public static void main (String [] args) {154 // TODO Auto-generated method stub155 RMB s = new RMB (); 156 System. out. println ("enter an integer that is less than 12 digits:"); 157 bytes in = new bytes (System. in); 158 double a = in. nextDouble (); 159 System. out. println ("reading method:"); 160 // connect the Chinese character strings in the integer part with the Chinese character strings in the decimal part to the 161 System. out. println (s. zhengDivade (s. divadeZheng (a) + s. toStr1 (s. divadeXiao (a); 162} 163 164}