zoukankan      html  css  js  c++  java
  • 一、中文(大写)金额转换为数字金额

    一、阿拉伯数字与大写金额互换

            #region 阿拉伯数字与大写金额互换
            /// <summary>
            /// 将阿拉伯数字转为大写金额
            /// </summary>
            /// <param name="number"></param>
            /// <returns></returns>
            public static String ConvertToChinese(Decimal number)
            {
                number = number * 10000;
                var s = number.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
                var 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}");
                var r = Regex.Replace(d, ".", m => "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟万亿兆京垓秭穰"[m.Value[0] - '-'].ToString());
                return r;
            }
    
    
            /// <summary>
            /// 中文金额转换为数字金额(不包含负金额)
            /// </summary>
            /// <param name="chineseAmount">中文金额</param>
            /// <returns>数字金额</returns>
            public static string ChineseConvertToNumber(string chineseAmount)
            {
                if (string.IsNullOrEmpty(chineseAmount))
                {
                    return string.Empty;
                }
    
                chineseAmount.Replace("", "").Replace("", "").Replace("", "");//移除计算干扰文字
                var wordCharArray = chineseAmount.ToCharArray();
    
                double numberAmount = 0;//最终要返回的数字金额
    
                //金额位标志量
                bool wan = false;//表示有万位
                bool yi = false;//表示有亿位
                bool fen = false;//表示有分位
                bool jiao = false;//表示有角位
                bool shi = false;//表示有十位
                bool bai = false;//表示有百位
                bool qian = false;//表示有千位
    
                for (int i = (wordCharArray.Length - 1); i >= 0; i--)//从低位到高位计算
                {
                    double currentPlaceAmount = 0;//当前位金额值
    
                    //判断当前位对应金额标志量
                    if (wordCharArray[i] == '')
                    {
                        fen = true;
                        continue;
                    }
                    else if (wordCharArray[i] == '')
                    {
                        jiao = true;
                        fen = false;
                        continue;
                    }
                    else if (wordCharArray[i] == '')
                    {
                        fen = false;
                        jiao = false;
                        shi = true;
                        continue;
                    }
                    else if (wordCharArray[i] == '')
                    {
                        bai = true;
                        fen = false;
                        jiao = false;
                        shi = false;
                        continue;
                    }
                    else if (wordCharArray[i] == '')
                    {
                        qian = true;
                        fen = false;
                        jiao = false;
                        shi = false;
                        bai = false;
                        continue;
                    }
                    else if (wordCharArray[i] == '')
                    {
                        wan = true;
                        fen = false;
                        jiao = false;
                        shi = false;
                        bai = false;
                        qian = false;
                        continue;
                    }
                    else if (wordCharArray[i] == '亿')
                    {
                        yi = true;
                        wan = false;
                        fen = false;
                        jiao = false;
                        shi = false;
                        bai = false;
                        qian = false;
                        continue;
                    }
    
                    //根据标志量转换金额为实际金额
                    if (fen) currentPlaceAmount = ConvertNameToSmall(wordCharArray[i]) * 0.01;
                    else if (jiao)
                    {
                        currentPlaceAmount = ConvertNameToSmall(wordCharArray[i]) * 0.1;
                        jiao = false;
                    }
                    else if (shi) currentPlaceAmount = ConvertNameToSmall(wordCharArray[i]) * 10;
                    else if (bai) currentPlaceAmount = ConvertNameToSmall(wordCharArray[i]) * 100;
                    else if (qian) currentPlaceAmount = ConvertNameToSmall(wordCharArray[i]) * 1000;
                    else
                    {
                        currentPlaceAmount = ConvertNameToSmall(wordCharArray[i]);
                    }
    
                    //每万位处理
                    if (yi)
                    {
                        currentPlaceAmount = currentPlaceAmount * 100000000;
                    }
                    else if (wan)
                    {
                        currentPlaceAmount = currentPlaceAmount * 10000;
                    }
    
                    numberAmount += currentPlaceAmount;
                }
    
                return numberAmount.ToString();
            }
    
    
            /// <summary>
            ///  转换中文数字为阿拉伯数字
            /// </summary>
            /// <param name="chinese">中文数字</param>
            /// <returns></returns>
            private static int ConvertNameToSmall(char chinese)
            {
                int number = 0;
                switch (chinese.ToString())
                {
                    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: throw new Exception("中文金额数字不正确:" + chinese);
                }
                return number;
            }
    
            /// <summary>
            /// 阿拉伯数字转中文
            /// </summary>
            /// <param name="index"></param>
            /// <returns></returns>
            public static string NumericToZH(string index)
            {
               var indexInt = ToInt(index);
                if (indexInt > 10)
                {
                    return string.Empty;
                }
                string[] num = { "", "", "", "", "", "", "", "", "", "", "" };
                
                return num[indexInt];
            }
    
            #endregion
  • 相关阅读:
    Docker 基础 : 数据管理
    linux sudo 命令
    TeamCity : .NET Core 插件
    C# 文件下载之断点续传
    TeamCity : 配置 Build 过程
    TeamCity : Build 版本控制系统配置
    Git : SSH 协议服务器
    TeamCity : Build 基本配置
    C# 文件下载 : WinINet
    [翻译] TSMessages
  • 原文地址:https://www.cnblogs.com/fger/p/11165841.html
Copyright © 2011-2022 走看看