博客
关于我
简单实用算法——人民币金额大写转换
阅读量: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 error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>
    npm install CERT_HAS_EXPIRED解决方法
    查看>>
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 EEXIST File exists 的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 Failed to connect to github.com port 443 的解决方法
    查看>>
    npm install 报错 fatal: unable to connect to github.com 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>
    npm install 权限问题
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>
    npm install无法生成node_modules的解决方法
    查看>>
    npm install的--save和--save-dev使用说明
    查看>>
    npm node pm2相关问题
    查看>>
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>
    npm run build报Cannot find module错误的解决方法
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm run dev 和npm dev、npm run start和npm start、npm run serve和npm serve等的区别
    查看>>
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>