将人民币的数字表示转化成大写表示(VB.NET版)
2024-07-10 13:01:13
供稿:网友
'将人民币的数字表示转化成大写表示(vb.net版)
'本代码参考chenyu001
'将人民币的数字表示转化成大写表示(c#版)
'http://dev.csdn.net/article/28/28977.shtm
'改的不多,但愿这些改动没让原作者发怒
public class chinesenum
'输入字串
private _inputstring as string
'输出字串,如果无效则输出错误信息
private _outstring as string
'判断输出字串是否有效
private _valiad as boolean
public writeonly property inputstring() as string
set(byval value as string)
_inputstring = value
converttochinesenum()
end set
end property
public readonly property valiad() as boolean
get
return _valiad
end get
end property
public readonly property outstring() as string
get
return _outstring
end get
end property
private sub converttochinesenum()
dim numlist as string = "零壹贰叁肆伍陆柒捌玖"
dim rmblist as string = "分角元拾佰仟万拾佰仟亿拾佰仟万"
dim number as double = 0
dim tempoutstring as string
try
number = double.parse(me._inputstring)
catch ex as systemexception
me._outstring = "传入参数非数字!"
me._valiad = false
return
end try
if number > 9999999999999.99 then
me._valiad = false
me._outstring = "超出范围的人民币值"
return
end if
dim tempnumberstring as string = convert.toint64(number * 100).tostring()
dim tempnmberlength as integer = tempnumberstring.length
dim i as integer = 0
while i < tempnmberlength
dim onenumber as integer = int32.parse(tempnumberstring.substring(i, 1))
dim onenumberchar as string = numlist.substring(onenumber, 1)
dim onenumberunit as string = rmblist.substring(tempnmberlength - i - 1, 1)
if not (onenumberchar = "零") then
tempoutstring += onenumberchar + onenumberunit
else
if onenumberunit = "亿" orelse onenumberunit = "万" orelse onenumberunit = "元" orelse onenumberunit = "零" then
while tempoutstring.endswith("零")
tempoutstring = tempoutstring.substring(0, tempoutstring.length - 1)
end while
end if
if onenumberunit = "亿" orelse (onenumberunit = "万" andalso not tempoutstring.endswith("亿")) orelse onenumberunit = "元" then
tempoutstring += onenumberunit
else
dim tempend as boolean = tempoutstring.endswith("亿")
dim zeroend as boolean = tempoutstring.endswith("零")
if tempoutstring.length > 1 then
dim zerostart as boolean = tempoutstring.substring(tempoutstring.length - 2, 2).startswith("零")
if not zeroend andalso (zerostart orelse not tempend) then
tempoutstring += onenumberchar
end if
else
if not zeroend andalso not tempend then
tempoutstring += onenumberchar
end if
end if
end if
end if
i += 1
end while
while tempoutstring.endswith("零")
tempoutstring = tempoutstring.substring(0, tempoutstring.length - 1)
end while
while tempoutstring.endswith("元")
tempoutstring = tempoutstring + "整"
end while
me._outstring = tempoutstring
me._valiad = true
end sub
end class
'则试方法
dim m as new chinesenum
private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click
m.inputstring = me.textbox1.text
if m.valiad then
me.textbox2.text = m.outstring
me.textbox3.text = string.empty
else
me.textbox2.text = string.empty
me.textbox3.text = m.outstring
end if
end sub