Function ConvertToChineseCurrency(amount As Decimal) As String
' 定义中文数字和单位
Dim chineseNumbers() As String = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}
Dim chineseUnitsSmall() As String = {"", "拾", "佰", "仟"}
Dim chineseUnitsBig() As String = {"", "万", "亿", "兆"}
Dim chineseDecimalUnits() As String = {"角", "分"}
' 转换金额为字符串并处理小数部分
Dim amountStr As String = amount.ToString("0.00")
Dim integerPartStr As String = amountStr.Split(".")(0)
Dim decimalPartStr As String = amountStr.Split(".")(1)
' 初始化结果字符串
Dim result As New StringBuilder()
' 处理整数部分
Dim zeroCount As Integer = 0 ' 连续零的计数
For i As Integer = integerPartStr.Length - 1 To 0 Step -1
Dim digitIndex As Integer = Integer.Parse(integerPartStr.Substring(i, 1))
If digitIndex > 0 Then
' 添加非零数字及其单位
result.Insert(0, chineseNumbers(digitIndex) + chineseUnitsSmall((integerPartStr.Length - 1 - i) Mod 4))
zeroCount = 0 ' 重置连续零的计数
Else
zeroCount += 1 ' 增加连续零的计数
End If
' 添加大单位(万、亿等)
If (integerPartStr.Length - i) Mod 4 = 0 AndAlso i > 0 Then
result.Insert(0, chineseUnitsBig((integerPartStr.Length - i - 1) \ 4))
End If
Next
' 处理结果字符串开头的零
While result.Length > 0 AndAlso result(0) = chineseNumbers(0)
result.Remove(0, 1)
End While
' 添加“元”
If result.Length > 0 Then
result.Append("元")
Else
result.Append("零元")
End If
' 处理小数部分
If decimalPartStr <> "00" Then
For i As Integer = 0 To 1
Dim digitIndex As Integer = Integer.Parse(decimalPartStr.Substring(i, 1))
If digitIndex > 0 Then
result.Append(chineseNumbers(digitIndex) + chineseDecimalUnits(i))
End If
Next
Else
result.Append("整") ' 如果小数部分为0,则加“整”字
End If
Return result.ToString()
End Function