The Java implementation converts the number to RMB capital
- 2020-04-01 03:49:52
- OfStack
Rmb. Java
public class Rmb
{
double number; //Quantity of RMB
private String[] hanArr = {" zero ", " one ", " Ii. ", " 3 ", " boss ", " wu ", " lu ", " Retailer, ", " ", " nine " };//Chinese counting symbol
private String[] unitArr = {"", " Pick up ", " hk ", " micky "};//Chinese counting unit
private String[] unitArrs = {" wan ", " Hundred million ", " wan ", " mega ", " wan ", " Hundred million ", " wan ", " round "}; //Sequential add unit
//private String[] unitsArr = {" wan ", " Hundred million "}; // Chinese character count large units
public Rmb(){}
public Rmb(double number){
this.number = number;
}
public String[] divideNum(){
double num = Math.round(number*100);//Round the number
long integerPart = (long)num; //Make an integer with two decimal places to avoid subtraction
//double decimalsPart = num-integerPart;// In the fractional part, there is an error at the critical part
String decimalsPartStr;
long b = integerPart % 10; //Second place after the decimal point
long a = (integerPart/10) % 10;//After the decimal point
integerPart /= 100;
if(a==0 && b==0){
decimalsPartStr = null;
}else{
decimalsPartStr = "" + a + b;
}
return new String[] {String.valueOf(integerPart) , decimalsPartStr};
}
public String toHanStr(){
String[] results = new String[9]; //Used to temporarily store a string of Numbers after every four segments
String[] resultStrs = new String[9];//Used to temporarily store every four paragraphs converted into renminbi after the pronunciation
String result = "";//The final transformation result
String[] divideStr = divideNum(); //You get the long and decimal part strings decomposed into floating point Numbers. The first array element is an integer part string, and the second is a decimal part string
results[8] = divideStr[1];
for (int i=divideStr[0].length(), j=8; i>0&&j>0 ; i-=4,j--){
try{
results[j-1] = divideStr[0].substring(i-4, i);
}catch(Exception e){
results[j-1] = divideStr[0].substring(0, i);
break;
}
}
if(results[8] == null){
resultStrs[8] = " The whole ";
}else if(results[8].charAt(1) == '0'){
resultStrs[8] = hanArr[results[8].charAt(0) - 48] + " Angle "; //Change the number to Chinese capital according to ASCII code and hanArr array
}else{
resultStrs[8] = hanArr[results[8].charAt(0) - 48] + " Angle " + hanArr[results[8].charAt(1) - 48] + " points ";
}
for(int i=0; i<8; i++){
if(results[i] != null){
resultStrs[i] = "";
resultStrs[i] += hanArr[results[i].charAt(0) - 48] + unitArr[results[i].length() - 1]; //The number of units selected according to ASCII code and array length
for (int j=1; j<results[i].length(); j++ )
if(results[i].charAt(j-1) == '0' && results[i].charAt(j) != '0')
resultStrs[i] += " zero " + hanArr[results[i].charAt(j) - 48] + unitArr[results[i].length() - 1 - j]; //The number of units selected according to ASCII code and array length
else if(results[i].charAt(j) != '0' )
resultStrs[i] += hanArr[results[i].charAt(j) - 48] + unitArr[results[i].length() - 1 - j];
}
}
for (int i=0; i<8; i++ ){
if(resultStrs[i] != null){
result += resultStrs[i] + unitArrs[i];
}
}
result += resultStrs[8];
return result;
}
}
RmbTest. Java
public class RmbTest
{
public static void main(String[] args)
{
double l;
byte[] bye = new byte[50];
System.out.println(" Please enter the amount of RMB to be converted: ");
try{
System.in.read(bye);
}catch(Exception e){}
String s = new String(bye);
l = Double.parseDouble(s);
Rmb r = new Rmb(l);
s = r.toHanStr();
System.out.println(s);
}
}
The above is all the content of this article, hope to help you learn Java.