C#格式化处理字符串123456为"123,456"

c# 字符串格式化处理之一: 
 [STAThread]
        static void Main(string[] args)
        {
            long a = 123456;
            int j = a.ToString().Length;
            string b = null;
            string c = null;
            if(a.ToString().Length>3)
            {
                if(j%3 == 0)
                {
                    for(int i = 0;i                    {
                        b += a.ToString().Substring(i*3,3) + ",";
                    }
                    if(b.Substring(b.Length-1) == ",")
                    {
                        c = b.Substring(0,b.Length-1);
                    }
                }
                else if((j-1)%3 == 0)
                {
                    for(int i = 0;i                    {
                        b += a.ToString().Substring(i*3+1,3) + ",";
                    }
                    if(b.Substring(b.Length-1) == ",")
                    {
                        c = a.ToString().Substring(0,1) + "," + b.Substring(0,b.Length-1);
                    }
                }
                else if((j-2)%3 == 0)
                {
                    for(int i = 0;i                    {
                        b += a.ToString().Substring(i*3+2,3) + ",";
                    }
                    if(b.Substring(b.Length-1) == ",")
                    {
                        c = a.ToString().Substring(0,2) + "," + b.Substring(0,b.Length-1);
                    }
                }
            }
            else if(a.ToString().Length <= 3)
            {
                c = a.ToString();
            }
            Console.WriteLine(c);
            Console.Read();
        }

另外,这里有一个非常简单的方法:
       [STAThread]
        static void Main(string[] args)
        {
               int a = 123456;
               string b = a.ToString("n");
                Console.WriteLine(b);
                Console.Read();
        }
输出的结果是:  123,456.00
如果要把后边的 “.00”去掉,只需要写:
        [STAThread]
        static void Main(string[] args)
        {
               int a = 123456;
               string b = a.ToString("n");
               string c = b.SubString(0,b.Length-3);
                Console.WriteLine(c);
                Console.Read();
        }
就over了。
    
 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/25253744/viewspace-688477/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/25253744/viewspace-688477/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值