比如输入10060.03 输出 壹万零陆拾零叁分
键盘输入那里总搞不定,就是红色字体部分,本来的思路使用Scanner传入一个double数值,然后传入方法里,可惜行不通。。。。
package com.itheima;
import java.util.*;
public class RenMinBi {
private String[] shuZi = {"零","壹","贰","叁","肆","五","陆","柒","捌","玖"};
private String[] zhengDanWei = {"元","拾","佰","仟","万"};
private String[] xiaoDanWei = {"角","分"};
public static void main(String[] args) {
RenMinBi rmb = new RenMinBi();
//Scanner s = new Scanner(System.in);
//while(s.hasNext()){
//double d = s.nextDouble();
//}
String str1 = rmb.zhuanHua(1060.03);
System.out.println(str1);
}
public String zhuanHua(double num){
String str = String.valueOf(num);
String[] num1 = str.split("\\.");
int length = num1[0].length();
int length1 = num1[1].length();
String resault = "";
int sum = 0;
for(int i = 0;i < length;i++){
int num2 = num1[0].charAt(i)-48;
if(num2 == 0){
sum++;
}
if(num2 != 0){
resault = resault + shuZi[num2] + zhengDanWei[length-1-i];
if(sum > 0){
sum = 0;
}
}
else
{
if(sum > 1 || i == length-1){
resault = resault;
}
else{
resault = resault + shuZi[num2];
}
}
}
//resault = resault + "元";
for(int j = 0;j < length1;j++){
int num3 = num1[1].charAt(j)-48;
if( num3 != 0){
resault = resault +shuZi[num3] + xiaoDanWei[j];
}
else{
if(j == length1-1){
resault = resault;
}
else{resault = resault + shuZi[num3];}
}
}
return resault;
}
}
|
|