分享

人民币大写金额与阿拉伯数字的相互转化

 F2967527 2022-10-19 发布于北京

需求描述

对于从事金融或者会计行业的朋友,最常遇到的问题就是人民币大写金额与阿拉伯数字的相互转化问题。毕竟,对于发票、合同等正规文件中,需要使用人民币大写金额,所以就需要将阿拉伯数字转化为大写金额;但是针对于大写金额,我们又没有办法直接进行求和、相加减的运算操作,所以我们在计算金额前,又需要将大写金额转化为阿拉伯数字。

那么如何实现快速切换人民币大写金额与阿拉伯数字就显得尤为重要!

需求分析

人民币大写金额与阿拉伯数字的切换可以分为以下方面:

  • 将数字转为大写金额,可以通过设置单元格的格式为'中文大写数字'(具体步骤:选中单元格->数字->其他数字格式->特殊->中文大写数字),通过设置单元格格式实现的中文金额大写可以直接运算

  • 如果不希望修改格式,可以通过自定义函数使用'WorksheetFunction.Text(source.Text, '[DBNum2]')',这种方式会把数字转化为中文大写,如果需要显示元角分,需要进行特殊处理

  • 将大写金额转化为数字,可以通过建立阿拉伯数字与大写金额的匹配,然后通过遍历替换,得到每一个大写金额对应的阿拉伯数字,实现切换。这里需要特殊处理人民币的分位符如'拾、佰、仟、万、亿、兆、元、角、分、零、整'

01

阿拉伯数字转人民币大写金额

'将阿拉伯数字转化为中文大写数字,即金额大写Function NumberToRMB(source As Range) '通过Excel内置Text函数实现数字转中文大写 result = WorksheetFunction.Text(source.Text, '[DBNum2]') '针对带小数的数字,特殊处理金额 If InStr(result, '.') Then yuan = Mid(result, 1, InStr(result, '.') - 1) jiao = Mid(result, InStr(result, '.') + 1, 1) fen = Mid(result, InStr(result, '.') + 2, 1) '处理元、角、分 If yuan <> '' Then NumberToRMB = yuan & '元' End If If jiao <> '' Then NumberToRMB = NumberToRMB & jiao & '角' End If If fen <> '' Then NumberToRMB = NumberToRMB & fen & '分' End If Else NumberToRMB = result & '元' End IfEnd Function

图片

02

人民币大写金额转阿拉伯数字

'将金额大写(中文大写数字)转化为阿拉伯数字Function RMBToNumber(source As Range)    Dim dict As Object    Dim unitDict As Object    Set dict = CreateObject('Scripting.Dictionary')    Set unitDict = CreateObject('Scripting.Dictionary')    '预定义中文大写与阿拉伯数字的对应    chineseNum = '零壹贰叁肆伍陆柒捌玖'    num = '0123456789'    '将中文大写与数字加入到集合dict中,便于后续匹配    For i = 1 To Len(num)        dict.Add Mid(chineseNum, i, 1), Mid(num, i, 1)    Next i    '预定义单位    chinese_uom = '拾、佰、仟、万、亿、兆、元、角、分、零、整'    num_uom = '10、100、1000、10000、100000000、1000000000000、1、0.1、0.01、1、1'    chinese_uom_split = Split(chinese_uom, '、')    num_uom_split = Split(num_uom, '、')    For i = 0 To UBound(chinese_uom_split)        unitDict.Add chinese_uom_split(i), num_uom_split(i)    Next    temp = 0    '遍历中文大写,依次替换每一个字符    sourceText = source.Text    For i = 1 To Len(sourceText)        Debug.Print Mid(sourceText, i, 1)        current = Mid(sourceText, i, 1)        '判断是否是常规大写,直接替换        If dict.exists(current) Then            Debug.Print dict(current)            temp = temp + CInt(dict(current))        '如果是单位大写,特殊处理        ElseIf unitDict.exists(current) Then            If current = '兆' Then                result = result + temp                result = result * unitDict(current)                zhao = result                result = 0            ElseIf current = '亿' Then                result = result + temp                result = result * unitDict(current)                yi = result                result = 0            ElseIf current = '万' Then                result = result + temp                result = result * unitDict(current)                wan = result                result = 0            Else                temp = temp * unitDict(current)                result = result + temp            End If            temp = 0        End If        Debug.Print result    Next    result = zhao + yi + wan + result + temp    '输出最终转化后的结果    RMBToNumber = resultEnd Function

图片

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多