美文网首页
中文(大写)金额转换为数字金额

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

作者: 天然豆浆机 | 来源:发表于2018-10-23 22:07 被阅读0次

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

            /// <summary>
            /// 中文金额转换为数字金额(不包含负金额)
            /// </summary>
            /// <param name="chineseAmount">中文金额</param>
            /// <returns>数字金额</returns>
            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();
            }
    
            /// <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;
            }
    

    相关文章

      网友评论

          本文标题:中文(大写)金额转换为数字金额

          本文链接:https://www.haomeiwen.com/subject/pijzzftx.html