如何使用VB把数字转成中文字符串?
根据数字的读法,写了一个把数字转成中文字符串的程序
参数一为数字
参数二为是不是反回人民币大写
参数三为是不是直接读数字,否则带有十百等单位
参数四为设置小数点后面的位数,默认为4
使用方法是
t=GetChinaNum(20005.000436, , , 7)'返回 “二千零五点零零零四三六”
t=GetChinaNum(2005.436, True, , 7)'返回“贰仟零伍元肆角肆分”
t=GetChinaNum(2005.436, , True, 7)'返加“二零零五点四三六”
下面是程序代码
FunctionGetChinaNum(otherNumAsDouble,OptionalisRMBAsBoolean,OptionalnumOptionAsBoolean,OptionaldotNumAsInteger)AsString
OnErrorResumeNext
num=Trim(Str(Int(otherNum)))
IfisRMBThen
numwei="拾佰仟万拾佰仟亿拾佰仟"
numshu="零壹贰叁肆伍陆柒捌玖拾"
Else
numwei="十百千万十百千亿十百千"
numshu="零一二三四五六七八九十"
EndIf
IfotherNum<20AndotherNum>=10Then
num=Right(num,1)
GetChinaNum=Left(numwei,1)
EndIf
Fori=1ToLen(num)
bstr=Mid(num,i,1)
IfnumOptionThen
GetChinaNum=GetChinaNum+Mid(numshu,Val(bstr)+1,1)
Else
GetChinaNum=GetChinaNum+Mid(numshu,Val(bstr)+1,1)
Ifbstr="0"Then
IfMid(numwei,Len(num)-i,1)="万"OrMid(numwei,Len(num)-i,1)="亿"Then
DoWhileRight(GetChinaNum,1)="零"
GetChinaNum=Left(GetChinaNum,Len(GetChinaNum)-1)
Loop
GetChinaNum=GetChinaNum+Mid(numwei,Len(num)-i,1)
EndIf
Else
GetChinaNum=GetChinaNum+Mid(numwei,Len(num)-i,1)
EndIf
GetChinaNum=Replace(GetChinaNum,"零零","零")
EndIf
Nexti
IfnumOption=FalseThen
DoWhileRight(GetChinaNum,1)="零"
GetChinaNum=Left(GetChinaNum,Len(GetChinaNum)-1)
Loop
EndIf
IfisRMBThen
numrmb="元角分"
GetChinaNum=GetChinaNum+Mid(numrmb,1,1)
IfVal(num)<>otherNumThen
num=Trim(Str(Round(otherNum-Val(num),2)))
Fori=2ToLen(num)
bstr=Mid(num,i,1)
GetChinaNum=GetChinaNum+Mid(numshu,Val(bstr)+1,1)+Mid(numrmb,i,1)
Nexti
Else
GetChinaNum=GetChinaNum+"整"
EndIf
Else
IfVal(num)<>otherNumThen
IfdotNum=0ThendotNum=4
num=Trim(CStr(Round(otherNum-Val(num),dotNum)))
IfGetChinaNum=""ThenGetChinaNum="零"
GetChinaNum=GetChinaNum+"点"
Fori=2ToLen(num)
bstr=Mid(num,i,1)
GetChinaNum=GetChinaNum+Mid(numshu,Val(bstr)+1,1)
Nexti
EndIf
EndIf
EndFunction
OnErrorResumeNext
num=Trim(Str(Int(otherNum)))
IfisRMBThen
numwei="拾佰仟万拾佰仟亿拾佰仟"
numshu="零壹贰叁肆伍陆柒捌玖拾"
Else
numwei="十百千万十百千亿十百千"
numshu="零一二三四五六七八九十"
EndIf
IfotherNum<20AndotherNum>=10Then
num=Right(num,1)
GetChinaNum=Left(numwei,1)
EndIf
Fori=1ToLen(num)
bstr=Mid(num,i,1)
IfnumOptionThen
GetChinaNum=GetChinaNum+Mid(numshu,Val(bstr)+1,1)
Else
GetChinaNum=GetChinaNum+Mid(numshu,Val(bstr)+1,1)
Ifbstr="0"Then
IfMid(numwei,Len(num)-i,1)="万"OrMid(numwei,Len(num)-i,1)="亿"Then
DoWhileRight(GetChinaNum,1)="零"
GetChinaNum=Left(GetChinaNum,Len(GetChinaNum)-1)
Loop
GetChinaNum=GetChinaNum+Mid(numwei,Len(num)-i,1)
EndIf
Else
GetChinaNum=GetChinaNum+Mid(numwei,Len(num)-i,1)
EndIf
GetChinaNum=Replace(GetChinaNum,"零零","零")
EndIf
Nexti
IfnumOption=FalseThen
DoWhileRight(GetChinaNum,1)="零"
GetChinaNum=Left(GetChinaNum,Len(GetChinaNum)-1)
Loop
EndIf
IfisRMBThen
numrmb="元角分"
GetChinaNum=GetChinaNum+Mid(numrmb,1,1)
IfVal(num)<>otherNumThen
num=Trim(Str(Round(otherNum-Val(num),2)))
Fori=2ToLen(num)
bstr=Mid(num,i,1)
GetChinaNum=GetChinaNum+Mid(numshu,Val(bstr)+1,1)+Mid(numrmb,i,1)
Nexti
Else
GetChinaNum=GetChinaNum+"整"
EndIf
Else
IfVal(num)<>otherNumThen
IfdotNum=0ThendotNum=4
num=Trim(CStr(Round(otherNum-Val(num),dotNum)))
IfGetChinaNum=""ThenGetChinaNum="零"
GetChinaNum=GetChinaNum+"点"
Fori=2ToLen(num)
bstr=Mid(num,i,1)
GetChinaNum=GetChinaNum+Mid(numshu,Val(bstr)+1,1)
Nexti
EndIf
EndIf
EndFunction
本文地址:http://www.45fan.com/dnjc/68354.html