博客
关于我
简单实用算法——人民币金额大写转换
阅读量:460 次
发布时间:2019-03-06

本文共 781 字,大约阅读时间需要 2 分钟。

人民币金额大写转换

变量定义

  • number:待转换人民币金额

注:人民币金额单位为“元”,最多两位小数(即精确到分),可以为负数,具体大写规则参考下文的人民币大写的正确写法注意事项

算法代码(C#)

// 人民币金额大写转换public static string ConvertToChinese(decimal number){    string s = number.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");    s = s.Replace("0B0A", "@");    string d = Regex.Replace(s, @"(((?<=-)|(?!-)^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");    string r = Regex.Replace(d, ".", m => "负元空零壹贰叁肆伍陆柒捌玖空空空空空空整分角拾佰仟万亿兆京垓秭穰"[m.Value[0] - '-'].ToString());    return r;}

算法实现

核心思路是正则表达式和ASCII码值运算

  • 利用格式化输出把数字中间按顺序插入大写字母。
  • 使用正则表达式替换相邻的大写字母为空字符,并处理字符串中的零值。
  • 将数字和字母替换成中文(字母替换为数字的十进制数位的中文名称)。
  • 注意事项

    在“元”之后、应写“整”(或“正”)字;在“角”之后,可以不写“整”(或“正”)字;大写金额数字有“分”的,“分”后面不写“整”(或“正”)字。

    转载地址:http://xnefz.baihongyu.com/

    你可能感兴趣的文章
    npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
    查看>>
    npm安装教程
    查看>>
    npm报错Cannot find module ‘webpack‘ Require stack
    查看>>
    npm报错Failed at the node-sass@4.14.1 postinstall script
    查看>>
    npm报错fatal: Could not read from remote repository
    查看>>
    npm报错File to import not found or unreadable: @/assets/styles/global.scss.
    查看>>
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
    查看>>
    npm版本过高问题
    查看>>
    npm的“--force“和“--legacy-peer-deps“参数
    查看>>
    npm的安装和更新---npm工作笔记002
    查看>>
    npm的常用操作---npm工作笔记003
    查看>>
    npm的常用配置项---npm工作笔记004
    查看>>
    npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
    查看>>
    npm编译报错You may need an additional loader to handle the result of these loaders
    查看>>
    npm设置淘宝镜像、升级等
    查看>>
    npm设置源地址,npm官方地址
    查看>>
    npm设置镜像如淘宝:http://npm.taobao.org/
    查看>>
    npm配置安装最新淘宝镜像,旧镜像会errror
    查看>>
    NPM酷库052:sax,按流解析XML
    查看>>