下面是一个用vbscript实现的钱转换的代码,你可以把它改成java的 dim aa,bb function do_change(typeid) Dim wordarr(11), unitarr1(4), unitarr2(4), unitarr3(3) Dim int1 ' 整数部份 Dim point1 ' 小数部份 Dim int1_str ' 整数部份 Dim point1_str ' 小数部份 Dim str1, len1, i,j Dim idx1, idx2,idx3, unit_tagaa=testm.text1.value If Not IsNumeric(aa) Then ' check error msgbox "input is no number" Exit Function End If if typeid = "money" then str1 = FormatNumber(aa, 2, -1, 0) else str1 = FormatNumber(aa, 20, -1, 0) end if str1 = Replace(str1, ",", "") len1 = Len(str1)wordarr(1) = "零" wordarr(2) = "壹" wordarr(3) = "贰" wordarr(4) = "参" wordarr(5) = "肆" wordarr(6) = "伍" wordarr(7) = "陆" wordarr(8) = "柒" wordarr(9) = "捌" wordarr(10) = "玖" unitarr1(1) = "拾" unitarr1(2) = "佰" unitarr1(3) = "仟" unitarr2(1) = "万" unitarr2(2) = "亿" unitarr2(3) = "兆" unitarr3(1) = "角" unitarr3(2) = "分"j = 0 For i = 1 To len1 If mid(str1,i,1) = "." Then '将数字分成整数部分,及小数部分 j = i Exit For End If Next If j <> 0 Then int1 = Mid(str1, 1, j - 1) point1 = Mid(str1, j + 1, len1) Else int1 = str1 point1 = Null End Iflen1 = Len(int1) If len1 = 0 Or int1 = "0" Then int1_str = "零" Else j = 0 For i = len1 To 1 Step -1 j = j + 1 idx1 = mid(int1,j,1) idx2 = (i - 1) / 4 idx3 = (i - 1) Mod 4 If idx3 = 0 Then If idx1 <> 0 Then int1_str = RTrim(int1_str) & wordarr(idx1 + 1) unit_tag = "n" End If If idx2 <> 0 Then If unit_tag <> "y" Then int1_str = RTrim(int1_str) & unitarr2(idx2) unit_tag = "y" End If End If Else If idx1 <> 0 Then int1_str = RTrim(int1_str) & wordarr(idx1 + 1) & unitarr1(idx3) unit_tag = "n" Else If int1(j + 1) <> 0 Then int1_str = RTrim(int1_str) & wordarr(1) unit_tag = "n" End If End If End If Next End Iflen1 = Len(point1) If len1 = 0 Or point1 = "00" Then point1_str = "零" Else If typeid = "money" Then If mid(point1,1,1) = "0" Then point1_str = "零" End If For p_1 = 1 To len1 idx1 = mid(point1,p_1,1) If idx1 <> 0 Then point1_str = RTrim(point1_str) & wordarr(idx1 + 1) & unitarr3(p_1) End If Next else For p_1 = len1 To 1 step -1 idx1 = mid(point1,p_1,1) If idx1 <> 0 Then point1_str = wordarr(idx1 + 1) & RTrim(point1_str) end if next End If End IfIf typeid = "money" Then If int1_str <> "零" Or j = 0 Then bb = RTrim(int1_str) & "元" End If If point1_str <> "零" Then bb = RTrim(bb) & RTrim(point1_str) & "整" Else bb = RTrim(bb) & "整" End If Else bb = RTrim(int1_str) If point1_str <> "零" and trim(point1_str) <>"" Then bb = RTrim(bb) & "点" & RTrim(point1_str) End If End If testm.text2.value = bb End Function
public class RMB{
public static void main(String[] args){ String s = numtochinese("5225.27"); System.out.println(s);} public static String numtochinese(String input){ String s1="零壹贰叁肆伍陆柒捌玖"; String s4="分角整元拾佰仟万拾佰仟亿拾佰仟"; String temp=""; String result=""; if (input==null) return "输入字串不是数字串只能包括以下字符(′0′~′9′,′.′),输入字串最大只能精确到仟亿,小数点只能两位!"; temp=input.trim(); float f; try{ f=Float.parseFloat(temp); }catch(Exception e){return "输入字串不是数字串只能包括以下字符(′0′~′9′,′.′),输入字串最大只能精确到仟亿,小数点只能两位!";} int len=0; if (temp.indexOf(".")==-1) len=temp.length(); else len=temp.indexOf("."); if(len>s4.length()-3) return("输入字串最大只能精确到仟亿,小数点只能两位!"); int n1,n2=0; String num=""; String unit=""; //for(int i=0;iif(i>len+2){break;} for(int i=0;i<len+3;i++){ if(i==len) {continue;} n1=Integer.parseInt(String.valueOf(temp.charAt(i))); num=s1.substring(n1,n1+1); n1=len-i+2; unit=s4.substring(n1,n1+1); result=result.concat(num).concat(unit); } if ((len==temp.length())||(len==temp.length()-1)) result=result.concat("整"); if (len==temp.length()-2) result=result.concat("零分"); return result; } }
dim aa,bb
function do_change(typeid)
Dim wordarr(11), unitarr1(4), unitarr2(4), unitarr3(3)
Dim int1 ' 整数部份
Dim point1 ' 小数部份
Dim int1_str ' 整数部份
Dim point1_str ' 小数部份
Dim str1, len1, i,j
Dim idx1, idx2,idx3, unit_tagaa=testm.text1.value
If Not IsNumeric(aa) Then ' check error
msgbox "input is no number"
Exit Function
End If
if typeid = "money" then
str1 = FormatNumber(aa, 2, -1, 0)
else
str1 = FormatNumber(aa, 20, -1, 0)
end if
str1 = Replace(str1, ",", "")
len1 = Len(str1)wordarr(1) = "零"
wordarr(2) = "壹"
wordarr(3) = "贰"
wordarr(4) = "参"
wordarr(5) = "肆"
wordarr(6) = "伍"
wordarr(7) = "陆"
wordarr(8) = "柒"
wordarr(9) = "捌"
wordarr(10) = "玖"
unitarr1(1) = "拾"
unitarr1(2) = "佰"
unitarr1(3) = "仟"
unitarr2(1) = "万"
unitarr2(2) = "亿"
unitarr2(3) = "兆"
unitarr3(1) = "角"
unitarr3(2) = "分"j = 0
For i = 1 To len1
If mid(str1,i,1) = "." Then '将数字分成整数部分,及小数部分
j = i
Exit For
End If
Next
If j <> 0 Then
int1 = Mid(str1, 1, j - 1)
point1 = Mid(str1, j + 1, len1)
Else
int1 = str1
point1 = Null
End Iflen1 = Len(int1)
If len1 = 0 Or int1 = "0" Then
int1_str = "零"
Else
j = 0
For i = len1 To 1 Step -1
j = j + 1
idx1 = mid(int1,j,1)
idx2 = (i - 1) / 4
idx3 = (i - 1) Mod 4
If idx3 = 0 Then
If idx1 <> 0 Then
int1_str = RTrim(int1_str) & wordarr(idx1 + 1)
unit_tag = "n"
End If
If idx2 <> 0 Then
If unit_tag <> "y" Then
int1_str = RTrim(int1_str) & unitarr2(idx2)
unit_tag = "y"
End If
End If
Else
If idx1 <> 0 Then
int1_str = RTrim(int1_str) & wordarr(idx1 + 1) & unitarr1(idx3)
unit_tag = "n"
Else
If int1(j + 1) <> 0 Then
int1_str = RTrim(int1_str) & wordarr(1)
unit_tag = "n"
End If
End If
End If
Next
End Iflen1 = Len(point1)
If len1 = 0 Or point1 = "00" Then
point1_str = "零"
Else
If typeid = "money" Then
If mid(point1,1,1) = "0" Then
point1_str = "零"
End If
For p_1 = 1 To len1
idx1 = mid(point1,p_1,1)
If idx1 <> 0 Then
point1_str = RTrim(point1_str) & wordarr(idx1 + 1) & unitarr3(p_1)
End If
Next
else
For p_1 = len1 To 1 step -1
idx1 = mid(point1,p_1,1)
If idx1 <> 0 Then
point1_str = wordarr(idx1 + 1) & RTrim(point1_str)
end if
next
End If
End IfIf typeid = "money" Then
If int1_str <> "零" Or j = 0 Then
bb = RTrim(int1_str) & "元"
End If
If point1_str <> "零" Then
bb = RTrim(bb) & RTrim(point1_str) & "整"
Else
bb = RTrim(bb) & "整"
End If
Else
bb = RTrim(int1_str)
If point1_str <> "零" and trim(point1_str) <>"" Then
bb = RTrim(bb) & "点" & RTrim(point1_str)
End If
End If
testm.text2.value = bb
End Function
public static void main(String[] args){
String s = numtochinese("5225.27");
System.out.println(s);}
public static String numtochinese(String input){
String s1="零壹贰叁肆伍陆柒捌玖";
String s4="分角整元拾佰仟万拾佰仟亿拾佰仟";
String temp="";
String result="";
if (input==null) return "输入字串不是数字串只能包括以下字符(′0′~′9′,′.′),输入字串最大只能精确到仟亿,小数点只能两位!";
temp=input.trim();
float f;
try{
f=Float.parseFloat(temp); }catch(Exception e){return "输入字串不是数字串只能包括以下字符(′0′~′9′,′.′),输入字串最大只能精确到仟亿,小数点只能两位!";}
int len=0;
if (temp.indexOf(".")==-1) len=temp.length();
else len=temp.indexOf(".");
if(len>s4.length()-3) return("输入字串最大只能精确到仟亿,小数点只能两位!");
int n1,n2=0;
String num="";
String unit=""; //for(int i=0;iif(i>len+2){break;}
for(int i=0;i<len+3;i++){
if(i==len) {continue;}
n1=Integer.parseInt(String.valueOf(temp.charAt(i)));
num=s1.substring(n1,n1+1);
n1=len-i+2;
unit=s4.substring(n1,n1+1);
result=result.concat(num).concat(unit);
}
if ((len==temp.length())||(len==temp.length()-1)) result=result.concat("整");
if (len==temp.length()-2) result=result.concat("零分");
return result;
}
}