VB.net中,自定义函数:大写金额转换为数字处理方式

在VB.NET中实现大写金额转换为数字的函数需要根据中文大写金额的规则进行解析。以下是基于中文大写金额格式(如“壹佰贰拾元伍角整”)的实现方案,结合了多个参考代码中的逻辑。

Public Class CurrencyConverter Private Shared ReadOnly DigitMap As New Dictionary(Of String, Integer) From { {“零”, 0}, {“壹”, 1}, {“贰”, 2}, {“叁”, 3}, {“肆”, 4}, {“伍”, 5}, {“陆”, 6}, {“柒”, 7}, {“捌”, 8}, {“玖”, 9} } Private Shared ReadOnly UnitMap As New Dictionary(Of String, Decimal) From { {“分”, 0.01D}, {“角”, 0.1D}, {“元”, 1D}, {“拾”, 10D}, {“佰”, 100D}, {“仟”, 1000D}, {“万”, 10000D}, {“亿”, 100000000D} } Public Shared Function ChineseToDecimal(input As String) As Decimal input = input.Replace(“整”, “”).Replace(” “, “”) Dim total As Decimal = 0D Dim currentValue As Decimal = 0D Dim tempValue As Decimal = 0D Dim maxUnit As Decimal = 0D For Each c As Char In input Dim charStr As String = c.ToString()
If DigitMap.ContainsKey(charStr) Then currentValue = currentValue * 10 + DigitMap(charStr) ElseIf UnitMap.ContainsKey(charStr) Then Dim unitValue As Decimal = UnitMap(charStr)
‘ 处理万/亿等大单位 If unitValue >= 10000 Then total += (tempValue + currentValue) * unitValue tempValue = 0D currentValue = 0D maxUnit = unitValue ElseIf unitValue >= 1 Then ‘ 元/拾/佰/仟 tempValue += currentValue * unitValue currentValue = 0D Else ‘ 角/分 total += (tempValue + currentValue) * unitValue currentValue = 0D tempValue = 0D End If End If Next Return total + tempValue + currentValue End FunctionEnd Class

功能特性说明:
基础转换逻辑

使用字典映射中文数字与阿拉伯数字的对应关系

处理”元、角、分”等小数单位

支持”拾、佰、仟、万、亿”等大单位计算

特殊处理规则

自动忽略结尾的”整”字(如”壹佰元整”)

支持连续零的处理(如”壹万零伍佰元” → 10500)

正确处理复合单位(如”叁万伍仟元” → 35000)

使用示例:

Console.WriteLine(CurrencyConverter.ChineseToDecimal(“壹佰贰拾元伍角”)) ‘ 输出 120.5Console.WriteLine(CurrencyConverter.ChineseToDecimal(“叁万伍仟元整”)) ‘ 输出 35000Console.WriteLine(CurrencyConverter.ChineseToDecimal(“玖亿零伍万陆仟柒佰捌拾玖元”)) ‘ 输出 90056789

 

欢迎使用66资源网
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
7. 本站有不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别!

66源码网 » VB.net中,自定义函数:大写金额转换为数字处理方式

提供最优质的资源集合

立即查看 了解详情