Enter a floating-point value from the command line parameters and convert it to the uppercase Chinese amount, such as 123.45, which is converted to: one hundred twenty three yuan four corners and five points, with multiple zeros as long as one zero.
package 实验3;
public class Demo {
static String bigLetter[] = {
"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
// 货币单位
static String unit[] = {
"元", "拾", "佰", "仟", "万",
// 拾万位到仟万位
"拾", "佰", "仟",
// 亿位到万亿位
"亿", "拾", "佰", "仟"};
public static void main (String args[]) {
String tmp=args[0];
System.out.println(tmp);
String result []=tmp.split("\\."); //转义字符这里要注意下
int len =result.length;
String s=solve(result[0]);
String s1="";
if (len>1)
s1=fun(result[1]);
System.out.println(new StringBuffer(s).reverse()+s1);
}
public static String solve(String s) //处理整数部分
{
long x=Long.valueOf(s);
int ans=-1,flag=0,zz=0;
String a = "";
if(x==0)
return "零";
while (x>0)
{
ans++;
long m=x%10;
x=x/10;
if (m==0&&flag==1) {
a+=bigLetter[(int) m];
flag=0;
}else if (m==0&&flag==0) {
if(unit[ans]=="万")
zz=1;
if(unit[ans]=="亿")
zz=2;
continue;
}else if (m>0){
if (zz==1)
{
a+="万";
zz=0;
}
if(zz==2)
{
a+="亿";
zz=0;
}
flag=1;
a+=unit[ans];
a+=bigLetter[(int) m];
}
}
return a;
}
public static String fun (String s) {
//处理小数部分,规模小直接if就好了省事
int x=Integer.valueOf(s),ans=0,flag=0,zz=0;
if(x==0)
return "";
if(x<10&&s.length()==1)
return bigLetter[x]+"角";
int m=x%10;
x=x/10;
if (x!=0&&m==0)
{
return bigLetter[x]+"角";
}
if(x==0)
{
return bigLetter[m]+"分";
}
return bigLetter[x]+"角"+bigLetter[m]+"分";
}
}
The following is the result of the code:
10,000,000,010.12
Yibai one hundred $ 201 One two divide hair
1,000,000,000.12
one hundred one hundred million Mao One two divide
1,000,010,100.02
one hundred ten thousand zero $ 201 Yibai two divide
1000010100.2
one hundred ten thousand $ 201 Yibai zero angle II
0.00
zero
10,000
ten thousand