-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnumber2Chinese.java
128 lines (127 loc) · 3.45 KB
/
number2Chinese.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import java.util.Scanner;
import static java.lang.System.out;
/*
*输入: 4545.454
*输出:肆仟伍佰肆拾伍点肆伍肆
*/
public class number2Chinese {
public static void main(String[] args) {
String num,decimals="",theInteger="",result,finalResult;
char temp;
int i=0,j=0;
Scanner sc = new Scanner(System.in);
out.println("请输入一个数字.");
num = sc.next(); //因为使用double类型小数点后的精度会丢失,所以使用字符串来输入数字.
for(i=0;i<num.length();i++){ //16-24行用于处理整数部分,将整数部分保存到theInteger字符串中.
temp = num.charAt(i);
if(temp == '.'){
break;
}
else{
theInteger+=temp;
}
}
i+=1;
for(;i<num.length();i++){//26-29行用于处理小数部分,将小数部分保存到decimals字符串中.
temp = num.charAt(i);
decimals+=temp;
}
result = integerPart(theInteger);
if(decimals.length()>0){
finalResult = decimalPart(decimals,result);
out.println(finalResult+"元");
}
else{
out.println(result+"元");
}
}
static String integerPart(String s){
String result="",part1="",part2="",part3="";
//part1用于处理小于一万的部分,part2用于处理大于等于一万小于一亿的部分,part3用于处理大于等于一亿部分.
char temp,isZero='9';
int count=0,char2int;
String[] chineseNum = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
String[] units = {"","拾","佰","仟"};
int i=0,j=0;
for(i=s.length()-1;i>=0;i--){//因为数分割是按每四位一次的,所以将整数部分字符串从后往前,每四位分割一次.
temp = s.charAt(i);
if(count>=0 && count<4){
part1+=temp; //part1表示0-9999
}
else if(count>=4 && count<8){
part2+=temp; //part2表示10000-99999999
}
else if(count>=8 && count<12){
part3+=temp; //part3表示100000000-999999999999
}
count++;
}
if(part3.length()>0){//从最大的part3开始处理
for(i=part3.length()-1;i>=0;i--){//因为在分割字符串时是从后往前分割,所以保存在part3的数字是倒序的.
temp = part3.charAt(i); //用temp保存i位置的数字.
char2int = temp-48; //将temp对应的数字保存在char2int中.
if(char2int != 0 ){
result = result + chineseNum[char2int] + units[i];
}
else{
if(i>0){ //如果i==0时,下面判断时会越界所以在这里加个判断语句.
isZero = part3.charAt(i-1);//isZero判断是否出现连续的0;如果part3.charAt(i)==0
//并且part3.charAt(i-1)==0,则下面的那个不会执行.
}
if(isZero != '0'){
result = result + chineseNum[char2int];
}
}
}
result+="亿"; //等四个数字全部处理完后在result后加个"亿".
}
if(part2.length()>0){//同part3处理方法.
for(i=part2.length()-1;i>=0;i--){
temp = part2.charAt(i);
char2int = temp-48;
if(char2int != 0 ){
result = result + chineseNum[char2int] + units[i];
}
else{
if(i>0){
isZero = part2.charAt(i-1);
}
if(isZero != '0'){
result = result + chineseNum[char2int];
}
}
}
result+="万";
}
if(part1.length()>0){
for(i=part1.length()-1;i>=0;i--){
temp = part1.charAt(i);
char2int = temp-48;
if(char2int != 0 ){
result = result + chineseNum[char2int] + units[i];
}
else{
if(i>0){
isZero = part1.charAt(i-1);
}
if(isZero != '0'){
result = result + chineseNum[char2int];
}
}
}
}
return result;
}
static String decimalPart(String s,String result){
result+="点";
char temp;
int i=0,num2int;
String[] chineseNum = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
for(i=0;i<s.length();i++){
temp = s.charAt(i);
num2int = temp-48;
result+=chineseNum[num2int];
}
return result;
}
}