博客
关于我
简单实用算法——人民币金额大写转换
阅读量: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/

    你可能感兴趣的文章
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notes on Paul Irish's "Things I learned from the jQuery source" casts
    查看>>
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    NotImplementedError: Could not run torchvision::nms
    查看>>
    nova基于ubs机制扩展scheduler-filter
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    np.power的使用
    查看>>
    NPM 2FA双重认证的设置方法
    查看>>
    npm build报错Cannot find module ‘html-webpack-plugin‘解决方法
    查看>>
    npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
    查看>>
    npm build报错Cannot find module ‘webpack‘解决方法
    查看>>
    npm C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe` failed with exit code: 1
    查看>>