zoukankan      html  css  js  c++  java
  • 2018-4-29-C#-金额转中文大写

    title author date CreateTime categories
    C# 金额转中文大写
    lindexi
    2018-04-29 09:50:38 +0800
    2018-04-02 21:42:22 +0800
    C#

    今天看到一个库是把金额转中文大写,看起来很容易,所以我就自己写了。

    创建的项目是创建一个 dot net core 的项目,实际上这个项目可以创建为 Stand 的。

    首先创建类,这个类的构造传入一个 double 作为人民币

        public class Money
        {
            public Money(double money)
            {
                _money = money;
            }
    
            private double _money;
      }

    然后创建方法转换

            public string ToCapital()
            {
                if (Math.Abs(_money) < 0.0001)
                {
                    return "零元";
                }
    
                var str = GetIntPart();
                GetDecimalPart(str);
                return str.ToString();
            }

    其中 GetIntPart 是转换小数点前的部分,小数点之后的使用 GetDecimalPart 计算。

    然后创建一些使用的数组

            private static readonly List<char> Uppers = new List<char>()
            {
                '零',
                '壹',
                '贰',
                '叁',
                '肆',
                '伍',
                '陆',
                '柒',
                '捌',
                '玖'
            };
    
            private static readonly List<char> Units = new List<char>()
            {
                '分',
                '角'
            };
    
            private static readonly List<char> Grees = new List<char>()
            {
                '元',
                '拾',
                '佰',
                '仟',
                '万',
                '拾',
                '佰',
                '仟',
                '亿',
                '拾',
                '佰',
                '仟',
                '万',
                '拾',
                '佰'
            };

    转换小数点前的代码

               StringBuilder str = new StringBuilder();
                var money = _money;
            
                for (int i = 0; money > 0.99999; i++)
                {
                    var n = (int) (money % 10);
                    str.Insert(0,Grees[i]);
                    str.Insert(0,Uppers[n]);
                    money = money / 10;
                    money = money - n / 10.0;
                }

    但是这样转换得到的存在一些零,如输入 100 会输出 壹佰零拾零元 ,所以需要对输出转换

            private StringBuilder GetIntPart()
            {
                StringBuilder str = new StringBuilder();
                var money = _money;
    
                for (int i = 0; money > 0.99999; i++)
                {
                    var n = (int) (money % 10);
                    str.Insert(0, Grees[i]);
                    str.Insert(0, Uppers[n]);
                    money = money / 10;
                    money = money - n / 10.0;
                }
    
                str = str.Replace("零亿", "亿零");
                str = str.Replace("零万", "万零");
    
                str = str.Replace("零拾", "");
                str = str.Replace("零佰", "");
                str = str.Replace("零仟", "");
    
                str = str.Replace("零零", "");
                str = str.Replace("零零", "");
    
                str = str.Replace("零亿", "亿");
                str = str.Replace("零万", "");
                str = str.Replace("零元", "");
    
                return str;
            }
    

    转换小数的代码

            private void GetDecimalPart(StringBuilder str)
            {
                var money = _money * 100;
                for (int i = 0; i < 2; i++)
                {
                    var n = (int) (money % 10);
                    if (n != 0)
                    {
                        str.Insert(0, Units[i]);
                        str.Insert(0, Uppers[n]);
                    }
    
                    money = money / 10;
                }
            }
    

    所有代码

    <script src='https://gitee.com/lindexi/codes/w6bxlue9o14rv5nscjyhf20/widget_preview?title=Money'></script>

    代码请看 https://gitee.com/lindexi/codes/w6bxlue9o14rv5nscjyhf20

    参见:src/Money.php · 趋势软件/capital - 码云 Gitee.com

  • 相关阅读:
    算法面试题总结
    面试题目整理
    九月百度,迅雷,华为,阿里巴巴,最新校招笔试面试十题
    ubuntu 环境变量配置
    VM 共享设置
    五大常用算法之五:分支限界法
    五大常用算法之四:回溯法
    Python之路【第十六篇】Django基础
    Python之路【第十五篇】WEB框架
    Python之路【第十四篇】前端补充回顾
  • 原文地址:https://www.cnblogs.com/lindexi/p/12086401.html
Copyright © 2011-2022 走看看