2015年8月21日 星期五

vb.net 數字轉國字

'加到類的定義部分
Private Shared [cstr] As String() = {"零", "壹", "貳", "叁", "肆", "伍", _
"陸", "柒", "捌", "玖"}
Private Shared wstr As String() = {"", "", "拾", "佰", "仟", "萬", _
"拾", "佰", "仟", "億", "拾", "佰", _
"仟"}
'數字必須在12位整數以內的字符串
'調用方式如:Label1.Text=ConvertInt("數字字符串");
Public Function ConvertInt(str As String) As String
Dim len As Integer = str.Length
Dim i As Integer
Dim tmpstr As String, rstr As String
rstr = ""
i = 1
While i <= len
tmpstr = str.Substring(len - i, 1)
rstr = String.Concat([cstr](Int32.Parse(tmpstr)) + wstr(i), rstr)
System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While
rstr = rstr.Replace("拾零", "拾")
rstr = rstr.Replace("零拾", "零")
rstr = rstr.Replace("零佰", "零")
rstr = rstr.Replace("零仟", "零")
rstr = rstr.Replace("零萬", "萬")
i = 1
While i <= 6
rstr = rstr.Replace("零零", "零")
System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While
rstr = rstr.Replace("零萬", "零")
rstr = rstr.Replace("零億", "億")
rstr = rstr.Replace("零零", "零")
rstr += "圓整"
Return rstr
End Function