+ -
当前位置:首页 → 问答吧 → 求助.关于大写数字转阿拉伯问题

求助.关于大写数字转阿拉伯问题

时间:2011-12-11

来源:互联网

C# code

public  string UperNumberToLower(string srcStr)
        {

            srcStr = Regex.Replace(srcStr, @"(?<![零一二三四五六七八九])十", "一十");
            srcStr = Regex.Replace(srcStr, @"十(?![零一二三四五六七八九])", "十零");
            return Regex.Replace(srcStr, @"(?![千百])[零一二三四五六七八九十百千万]+", new MatchEvaluator(UpToLowerDelegate));
        }

        public  string UpToLowerDelegate(Match m)
        {
            string uNumber = m.Value;

            int total = 0;
            int unit = -1;
            int standNum = -1;

            List<string> listNum = new List<string>(new string[] { "", "", "", "", "", "", "", "", "", "" });
            List<string> listUnit = new List<string>(new string[] { "", "", "","" });
            List<int> listInt = new List<int>(new int[] { 10, 100, 1000, 10000 });

            for (int i = 0; i < uNumber.Length; i++)
            {
                string strChar = uNumber[i].ToString();
                if (listNum.IndexOf(strChar) > -1)
                {
                    standNum = listNum.IndexOf(strChar);
                }

                if (listUnit.IndexOf(strChar) > -1)
                {
                    if (listUnit.IndexOf(strChar) < unit)
                    {
                        total = total + standNum * listInt[listUnit.IndexOf(strChar)];
                    }
                    else
                    {
                        total = (total + standNum) * listInt[listUnit.IndexOf(strChar)];
                        unit = listUnit.IndexOf(strChar);
                    }
                    standNum = -1;

                }
                if (i == uNumber.Length - 1 && standNum != -1)
                {
                    total = total + standNum;
                }

            }

            return total.ToString();
        }



这个能处理成功,但是当字符不存在 十百千万的时候就只能读到最后一个数字,。。。 调试了半天也不知道怎么回事..
如:字符是"三十八"能正常转换为"38" ,但是如果是"三八"处理后的结果是"8"

求指教

作者: ckhker   发布时间: 2011-12-11

求指教啊

作者: ckhker   发布时间: 2011-12-11

没有单位的话,你中间计算total那块是不执行的。你转换的时候最好从个位开始转,判断有没有单位,没有的话根据所在位置看是第几位

作者: dreamboy0908   发布时间: 2011-12-12

前段时间有人阿拉伯转大写,现在大写转阿拉伯……我等下写个全套试试

作者: sunzongbao2007   发布时间: 2011-12-12

但是规矩要定好

作者: sunzongbao2007   发布时间: 2011-12-12