首页 > 其他 > 详细

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

时间:2019-07-10 20:04:25      阅读:119      评论:0      收藏: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.cnblogs.com/fger/p/11165841.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!