VB.NET 数字转换为汉字金额大写文字函数 小数最多为2位

代码
''' <summary>
''' 人民币大小写转换。
''' Add by pwm 2010.07.21
''' </summary>
''' <param name="r">货币金额</param>
''' <returns>人民币大写字符串</returns>
''' <remarks>超过两位的小数会自动进行四舍五入。</remarks>
Public Shared Function ToRMB(ByVal r As Decimal) As String
Try
If r = 0 Then
Return "零元"
End If

Dim cnShuzi() As String = {"", "", "", "", "", "", "", "", "", ""}
Dim cnDanwei() As String = {"", "", "", "", "", "", "", "拾万", "佰万", "仟万", _
"亿", "拾亿", "佰亿", "仟亿", "万亿", "拾万亿", "佰万亿", "仟万亿", "万万亿"}
Dim arr() As Char = Decimal.ToInt64(Decimal.Round(r, 2) * 100).ToString().ToCharArray()
Array.Reverse(arr)

Dim rmb As New stringBuilder
For i As Integer = 0 To arr.Length - 1
Dim num As Integer = CInt(arr(i).ToString)
If num <> 0 Then
If i > 6 And i < 10 And rmb.ToString().IndexOf("") >= 0 Then
rmb.Insert(
0, cnShuzi(num) + cnDanwei(i)(0))
ElseIf i > 10 And i < 14 And rmb.ToString().IndexOf("亿 ") >= 0 Then
rmb.Insert(
0, cnShuzi(num) + cnDanwei(i)(0))
ElseIf i > 14 And rmb.ToString().IndexOf("万亿 ") >= 0 Then
rmb.Insert(
0, cnShuzi(num) + cnDanwei(i).Substring(0, 2))
Else
rmb.Insert(
0, cnShuzi(num) + cnDanwei(i))
End If

Else
If i = 2 Then
If arr(1) = "0 " Then
rmb.Insert(
0, "")
Else
rmb.Insert(
0, "元零 ")
End If
If rmb.Length > 0 Then
If rmb(0) <> "" And rmb(0) <> "" Then
rmb.Insert(
0, "")
End If
End If
End If
End If

Next
If rmb(rmb.Length - 1) = "" Then
Return rmb.ToString() + ""
Else
Return rmb.ToString()
End If

Catch ex As Exception
Return "数值过大,无法转换! "
End Try

End Function

 

posted on 2010-07-21 23:03  pwm_1987  阅读(1938)  评论(0编辑  收藏  举报

导航