中文(大写)金额转换为数字金额

此方法已应用到项目中,如有优化建议欢迎提出,谢谢!
不含负金额

        /// 
        /// 中文金额转换为数字金额(不包含负金额)
        /// 
        /// 中文金额
        /// 数字金额
        public static string ChineseConvertToNumber(this 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();
        }

        /// 
        ///  转换中文数字为阿拉伯数字
        /// 
        /// 中文数字
        /// 
        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;
        }

你可能感兴趣的:(中文(大写)金额转换为数字金额)