小写中文数字和阿拉伯数字相互转换、数字转字母

1.中文数字转阿拉伯数字

    public int ConvertChinaToNumber(string data)
    {
            int number = 0;
            string[] numArr = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            string chinStr = "零一二三四五六七八九";
            string bigStr = "十百千万十百千亿";
            string pad = "1";

            if (data.StartsWith("十"))
            {
                number = 1;
            }
            int count = data.Length;
            for (int i = 0; i < count; i++)
            {
                string li = data.Substring(i, 1);
                int chinIndex = chinStr.IndexOf(li);
                int bigIndex = bigStr.IndexOf(li);
                if (chinIndex > -1)
                {
                    number += Convert.ToInt32(numArr[chinIndex]);
                }
                else if (bigIndex > -1)
                {
                    number *= Convert.ToInt32(pad.PadRight(bigIndex + 2, '0'));
                }
            }

            return number;
        }

2.阿拉伯数字转中文小写数字

        public string ConvertNumberToChina(int num)
        {
            string[] intArr = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", };
            string[] strArr = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九", };
            string[] Chinese = { "", "十", "百", "千", "万", "十", "百", "千", "亿" };
            char[] tmpArr = num.ToString().ToArray();
            string tmpVal = "";
            for (int i = 0; i < tmpArr.Length; i++)
            {
                tmpVal += strArr[tmpArr[i] - 48];//ASCII编码 0为48
                tmpVal += Chinese[tmpArr.Length - 1 - i];//根据对应的位数插入对应的单位
            }
            if (tmpVal.StartsWith("一十"))
            {
                tmpVal = tmpVal.Replace("一十", "十");
            }
            return tmpVal.TrimEnd("零".ToArray());
        }

3.数字转字母

        public static string NumberToLetter(int number)
        {
            string result = "";
            do
            {
                result += Convert.ToChar(65 + (number % 26 == 0 ? 26 : number % 26) - 1).ToString();
                if (number % 26 == 0) number--;
                number = number / 26;
            } while (number > 0);

            var charAry = result.ToCharArray();
            Array.Reverse(charAry);
            return new string(charAry);
        }

你可能感兴趣的:(小写中文数字和阿拉伯数字相互转换、数字转字母)