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

数值转换汉字数值问题, 请尊重楼主版权,转载注明出处!(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();
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值