將人民幣的數字表示轉化成大寫表示(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