C# 金额大写转小写

2 篇文章 0 订阅
using System;


namespace OCRTest
{
    /// <summary>
    /// C#金额大写转小写
    /// </summary>
    public static class RMBChineseConvertToNumber
    {
        /// <summary>
        /// 将传入的大写金额字符串转换为小写的阿拉伯数字
        /// </summary>
        /// <param name="str">传入的字符串</param>
        /// <returns></returns>
        public static int BigChineseConvertToSmall(string str)
        {
            int totalCount = 0;//所有数据算出来的和
            int beforeMillion = 0;//亿以前的和
            int beforeWan = 0;//亿-万之间的和
            int AfterWan = 0;//万以后的和
            int Onehundredmillion = 100000000;
            str = str.Replace("零", "").Replace("元", "").Replace("整", "");


            if (!string.IsNullOrEmpty(str))
            {
                if (str.Contains("亿"))
                {
                    string beforeyi = "";
                    string afteryi = "";
                    string[] arryi = str.Split(new string[] { "亿" }, StringSplitOptions.RemoveEmptyEntries);
                    beforeyi = arryi[0];
                    beforeMillion = beforeyi.ConvertNameToSmall() * Onehundredmillion;
                    if (arryi.Length == 1) { afteryi = string.Empty; } else { afteryi = arryi[1]; }
                    if (afteryi.Contains("万"))
                    {
                        string beforewan = "";
                        string afterwan = "";
                        string[] arrwan = afteryi.Split(new string[] { "万" }, StringSplitOptions.RemoveEmptyEntries);
                        beforewan = arrwan[0];
                        if (arrwan.Length == 1) { afterwan = string.Empty; } else { afterwan = arrwan[1]; }
                        beforeWan = GetTotalCount(beforeWan, beforewan, "before");
                        AfterWan = GetTotalCount(AfterWan, afterwan);
                    }
                    else
                    {
                        AfterWan = GetTotalCount(AfterWan, afteryi);
                    }
                }
                else if (str.Contains("万"))
                {
                    string beforewan = "";
                    string afterwan = "";
                    string[] arrwan = str.Split(new string[] { "万" }, StringSplitOptions.RemoveEmptyEntries);
                    beforewan = arrwan[0];
                    if (arrwan.Length == 1) { afterwan = string.Empty; } else { afterwan = arrwan[1]; }
                    beforeWan = GetTotalCount(beforeWan, beforewan, "before");
                    AfterWan = GetTotalCount(AfterWan, afterwan);
                }
                else
                {
                    AfterWan = GetTotalCount(AfterWan, str);
                }
            }
            //最后计算出总和
            totalCount = beforeMillion + beforeWan + AfterWan;
            return totalCount;
        }


        /// <summary>
        /// 将大写的中文名称转换为阿拉伯数字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int ConvertNameToSmall(this string str)
        {
            int number = 0;
            switch (str)
            {
                case "零": number = 0; break;
                case "壹": number = 1; break;
                case "贰": number = 2; break;
                case "叁": number = 3; break;
                case "肆": number = 4; break;
                case "伍": number = 5; break;
                case "陆": number = 6; break;
                case "柒": number = 7; break;
                case "捌": number = 8; break;
                case "玖": number = 9; break;
                default: break;
            }
            return number;
        }


        /// <summary>
        /// 提取计算的公共方法
        /// </summary>
        /// <param name="direction">万以前或者万以后</param>
        /// <param name="content">处理的内容</param>
        /// <param name="dir">"before" 会*10000</param>
        /// <returns></returns>
        public static int GetTotalCount(int direction, string content, string dir = null)
        {
            int wan = 10000;
            if (dir != "before") { wan = 1; };
            if (content.Contains("仟"))
            {
                string beforeqian = "";
                string afterqian = "";
                string[] arrqian = content.Split(new string[] { "仟" }, StringSplitOptions.RemoveEmptyEntries);
                beforeqian = arrqian[0];
                if (arrqian.Length == 1) { afterqian = string.Empty; } else { afterqian = arrqian[1]; }
                direction += beforeqian.ConvertNameToSmall() * 1000 * wan;
                if (afterqian.Contains("佰"))
                {
                    string beforebai = "";
                    string afterbai = "";
                    string[] arrbai = afterqian.Split(new string[] { "佰" }, StringSplitOptions.RemoveEmptyEntries);
                    beforebai = arrbai[0];
                    if (arrbai.Length == 1) { afterbai = string.Empty; } else { afterbai = arrbai[1]; }
                    direction += beforebai.ConvertNameToSmall() * 100 * wan;
                    if (afterbai.Contains("拾"))
                    {
                        string beforeshi = "";
                        string aftershi = "";
                        string[] arrshi = afterbai.Split(new string[] { "拾" }, StringSplitOptions.RemoveEmptyEntries);
                        beforeshi = arrshi[0];
                        if (arrshi.Length == 1) { aftershi = string.Empty; } else { aftershi = arrshi[1]; }
                        direction += beforeshi.ConvertNameToSmall() * 10 * wan;
                        aftershi = arrshi[1];
                        if (!string.IsNullOrEmpty(aftershi))
                        {
                            direction += aftershi.ConvertNameToSmall() * wan;
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(afterbai))
                        {
                            direction += afterbai.ConvertNameToSmall() * wan;
                        }
                    }
                }
                else if (afterqian.Contains("拾"))
                {
                    string beforeshi = "";
                    string aftershi = "";
                    string[] arrshi = afterqian.Split(new string[] { "拾" }, StringSplitOptions.RemoveEmptyEntries);
                    beforeshi = arrshi[0];
                    if (arrshi.Length == 1) { aftershi = string.Empty; } else { aftershi = arrshi[1]; }
                    direction += beforeshi.ConvertNameToSmall() * 10 * wan;


                    if (!string.IsNullOrEmpty(aftershi))
                    {
                        direction += aftershi.ConvertNameToSmall() * wan;
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(afterqian))
                    {
                        direction += afterqian.ConvertNameToSmall() * wan;
                    }
                }
            }
            else if (content.Contains("佰"))
            {
                string beforebai = "";
                string afterbai = "";
                string[] arrbai = content.Split(new string[] { "佰" }, StringSplitOptions.RemoveEmptyEntries);
                beforebai = arrbai[0];
                if (arrbai.Length == 1) { afterbai = string.Empty; } else { afterbai = arrbai[1]; }
                direction += beforebai.ConvertNameToSmall() * 100 * wan;
                if (afterbai.Contains("拾"))
                {
                    string beforeshi = "";
                    string aftershi = "";
                    string[] arrshi = afterbai.Split(new string[] { "拾" }, StringSplitOptions.RemoveEmptyEntries);
                    beforeshi = arrshi[0];
                    if (arrshi.Length == 1) { aftershi = string.Empty; } else { aftershi = arrshi[1]; }
                    direction += beforeshi.ConvertNameToSmall() * 10 * wan;


                    if (!string.IsNullOrEmpty(aftershi))
                    {
                        direction += aftershi.ConvertNameToSmall() * wan;
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(afterbai))
                    {
                        direction += afterbai.ConvertNameToSmall() * wan;
                    }
                }
            }
            else if (content.Contains("拾"))
            {
                string beforeshi = "";
                string aftershi = "";
                string[] arrshi = content.Split(new string[] { "拾" }, StringSplitOptions.RemoveEmptyEntries);
                beforeshi = arrshi[0];
                if (arrshi.Length == 1) { aftershi = string.Empty; } else { aftershi = arrshi[1]; }
                direction += beforeshi.ConvertNameToSmall() * 10 * wan;
                if (!string.IsNullOrEmpty(aftershi))
                {
                    direction += aftershi.ConvertNameToSmall() * wan;
                }
            }
            else
            {
                direction += content.ConvertNameToSmall() * wan;
            }
            return direction;
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值