算法_数字转换为汉字数值_自己写了好多种_自认这是最简单的算法_

来源:互联网 发布:淘宝pc端访客怎么推广 编辑:程序博客网 时间:2024/05/22 18:22

数值转换汉字数值问题, 请尊重楼主版权,转载注明出处!(2个小时尝试4种方式后的最简结果...) 

class MyClass    {        static string Rel(int number)        {            //结果            string resule = "";            //用作替换数字的字符数组            string[] rep = new string[]             { "零", "一", "二", "三", "四",                "五", "六", "七", "八", "九", };            //用于添加单位的数组            string[] unit = new string[]             { "", "十", "百", "千", "万","十"            ,"百" ,"千" ,"亿","十","百" ,"千"};            //取数字位数            int l= number.ToString().Length;            //循环取最后以为数字处理字符转换            for (int i = 0; i <l; i++)            {                //取最后位数值                int temp = number % 10;                //取剩余位                number = number/10;                //判断当前最后位为0                if (temp == 0)                {                    //判断万位添加单位 万                    if (i == 4)                        resule += unit[4];                    //判断亿位添加单位 亿                    if (i == 8)                        resule += unit[8];                    //判断当前最后位是否需要加 零                    if(resule!=""&&resule[resule.Length-1]!= '零'&& resule[resule.Length - 1] != '万'&& resule[resule.Length - 1] != '亿')                        resule += rep[temp];                                   }                else                {                    //当前位不是0 添加单位 添加数值                    resule += unit[i];                    resule += rep[temp];                }                }            //定义中间变量 倒叙结果            string str = resule;            resule = "";            for (int i = 0; i < str.Length; i++)            {                resule += str[str.Length - 1 - i];            }            return resule;        }           static void Main(string[] args)        {            Console.WriteLine(Rel(1560850500));;                       Console.ReadLine();        }