服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

小写金额转成大写算法


  小写金额转成大写算法

写了几天了,最终转的还是有问题.请高手帖出算法或代码.
---------------------------------------------------------------

我用c++写过一个,代码不在身边.
不过可以提示一下,将金额4位一分,比如1234567可以拆分为1234和567.另外,数字中间有零的话必须写零,但要注意多个零的情况.详细的怎么写你可以问问做财务的人.
---------------------------------------------------------------

我以前写过
说一下思路
先把数字分成整数小数
假如整数100909应该是十万零九百零九
有数组
一个:零,一,二,三,四,五,六,七,八,九
一个:元,角,分,厘
一个:元,十,百,千,万,十,百,千,亿
然后依次去取就可以了
100909
上面会拼出
一十零零九百零九元
你碰到万位补万,两个零变成一个


---------------------------------------------------------------

有个比较笨点的:
判断每个字符,然后条件if(c.equals("1")){.....}
依此类推。。。。


---------------------------------------------------------------

http://www.csdn.net/develop/read_article.asp?id=15823
---------------------------------------------------------------

从csdn上整理出五个方案,请参考:

1、(java)
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;i<temp.length();i++){
if(i>len+2){break;}
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;
}


2、(jsp)
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<meta name="generator" content="microsoft frontpage 4.0">
<meta name="progid" content="frontpage.editor.document">
<title>转换大写金额</title>
<style type=text/css>
input { border-right: #c0d3dc 1px solid; border-top: #c0d3dc 1px solid; border-left: #c0d3dc 1px solid; border-bottom: #c0d3dc 1px solid; font-family: verdana; height: 19px}
body {font-family:arial,helvetica,tahoma; font-size: 9pt; color:´094a82´}
</style>
<bgsound id=bgsoundone src="" loop=1>
</head>
<body>
<script language="vbscript">
dim hj,hji
sub document_onkeypress()
if window.event.srcelement.id="num" and window.event.keycode="13" then
call retu_cn()
end if
end sub
sub window_onload()
document.fmsubmit.t1.focus()
end sub
sub retu_cn()
dim n,m_hj,s,ch,ch2,ch3,num
num=replace(trim(document.fmsubmit.t1.value),",","",1,-1,1)
if num="" then
msgbox "您还没有输入金额!"
document.fmsubmit.t1.focus()
exit sub
end if
if isnumeric(num)=false then
msgbox "您输入的不是金额!"
document.fmsubmit.t1.focus()
exit sub
end if
n=formatnumber(num,2,-1)
num=n
n=replace(n,",","",1,-1,1)
if len(n)>15 then
msgbox "金额必须小于千亿!"
document.fmsubmit.t1.focus()
exit sub
end if
hj=space(0)
s="840"
ch="壹贰叁肆伍陆柒捌玖"
ch2="仟佰拾 "
ch3="亿万元"
for x=1 to 3
m=cstr(int(n/10^formatnumber(mid(s,x,1),0,-1)))
m=space(4-len(m))+m
m_hj=space(0)
if m<>" 0" then
for y=1 to 4
if mid(m,y,1)<>space(1) and mid(m,y,1)<>"0" then
m_hj=trim(m_hj+mid(ch,formatnumber(mid(m,y,1),0,-1),1)+mid(ch2,y,1))
else
if mid(m,y,1)=space(1) or mid(m,y,1)="0" then
m_hj=m_hj
if right(m_hj,1)="零" then
m_hj=m_hj+space(0)
else
m_hj=m_hj+"零"
end if
else
m_hj=m_hj
end if
end if
next
if right(m_hj,1)="零" then
m_hj=mid(m_hj,1,len(m_hj)-1)+mid(ch3,x,1)
if x=3 then
m_hj=m_hj+space(0)
else
m_hj=m_hj+"零"
end if
else
m_hj=m_hj+mid(ch3,x,1)
end if
if right(hj,1)="零" and mid(m_hj,1,1)="零" then
hj=hj+mid(m_hj,2,len(m_hj)-1)
else
hj=hj+m_hj
end if
else
if x=3 then
if right(hj,1)="零" then
if trim(hj)="零" then
hj=hj+space(0)
else
hj=mid(hj,1,len(hj)-1)+"元"
end if
else
if trim(hj)="零" then
hj=hj+space(0)
else
hj=hj+"元"
end if
end if
else
if right(hj,1)="零" then
hj=hj+space(0)
else
hj=hj+"零"
end if
end if
end if
if n-int(n)=0 then

扫描关注微信公众号