C#中string.Format(string,object)方法

示例代码:
class Program
    {
        enumColor {Yellow = 1, Blue, Green};
        staticDateTime thisDate =DateTime.Now;

        publicstatic void Main()
        {
            //Store the output of the String.Format method in a string.
            strings = "";

            Console.Clear();
            //Format a negative integer or floating-point number in variousways.
            Console.WriteLine("StandardNumeric Format Specifiers");
            s= String.Format(
                "(C)Currency: . . . . . . . . {0:C}\n" +
                "(D)Decimal:. . . . . . . . . {0:D}\n" +
                "(E)Scientific: . . . . . . . {1:E}\n" +
                "(F)Fixed point:. . . . . . . {1:F}\n" +
                "(G)General:. . . . . . . . . {0:G}\n" +
                   (default):.. . . . . . . {0} (default = 'G')\n" +
                "(N)Number: . . . . . . . . . {0:N}\n" +
                "(P)Percent:. . . . . . . . . {1:P}\n" +
                "(R)Round-trip: . . . . . . . {1:R}\n" +
                "(X)Hexadecimal:. . . . . . . {0:X}\n",
                -123,-123.45f);
            
            Console.WriteLine(s);

            // Format the current date in various ways.
            
            Console.WriteLine("StandardDateTime Format Specifiers");
            s= String.Format(
                "(d)Short date: . . . . . . . {0:d}\n" +
                "(D)Long date:. . . . . . . . {0:D}\n" +
                "(t)Short time: . . . . . . . {0:t}\n" +
                "(T)Long time:. . . . . . . . {0:T}\n" +
                "(f)Full date/short time: . . {0:f}\n" +
                "(F)Full date/long time:. . . {0:F}\n" +
                "(g)General date/short time:. {0:g}\n" +
                "(G)General date/long time: . {0:G}\n" +
                   (default):.. . . . . . . {0} (default = 'G')\n" +
                "(M)Month:. . . . . . . . . . {0:M}\n" +
                "(R)RFC1123:. . . . . . . . . {0:R}\n" +
                "(s)Sortable: . . . . . . . . {0:s}\n" +
                "(u)Universal sortable: . . . {0:u} (invariant)\n" +
                "(U)Universal sortable: . . . {0:U}\n" +
                "(Y)Year: . . . . . . . . . . {0:Y}\n",
                thisDate);

            Console.WriteLine(s);
            //Format a Color enumeration value in various ways.
            Console.WriteLine("StandardEnumeration Format Specifiers");
            s= String.Format(
                "(G)General:. . . . . . . . . {0:G}\n" +
                   (default):.. . . . . . . {0} (default = 'G')\n" +
                "(F)Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
                "(D)Decimal number: . . . . . {0:D}\n" +
                "(X)Hexadecimal:. . . . . . . {0:X}\n",
                Color.Green);      
            Console.WriteLine(s);

            Console.ReadLine();
        }
        
    }


以上代码结果:
Standard Numeric Format Specifiers
(C) Currency: . . . .. . . .-123.00
(D) Decimal:. . . . . . . . . -123
(E) Scientific: . . . . . . . -1.234500E+002
(F) Fixed point:. . . . . . . -123.45
(G) General:. . . . . . . . . -123
    (default):.. . . . . . . -123 (default ='G')
(N) Number: . . . . . . . . . -123.00
(P) Percent:. . . . . . . . . -12,345.00%
(R) Round-trip: . . . . . . . -123.45
(X) Hexadecimal:. . . . . . . FFFFFF85

Standard DateTime Format Specifiers
(d) Short date: . . . . . . . 2008-11-1
(D) Long date:. . . .. . . . 2008111
(t) Short time: . . . . . . . 23:39
(T) Long time:. . . . . . . . 23:39:05
(f) Full date/shorttime: . . 200811123:39
(F) Full date/longtime:. . . 200811123:39:05
(g) General date/short time:. 2008-11-1 23:39
(G) General date/long time: . 2008-11-123:39:05
    (default):.. . . . . . . 2008-11-1 23:39:05 (default ='G')
(M) Month:. . . . . .. . . . 111
(R) RFC1123:. . . . . . . . . Sat, 01 Nov 2008 23:39:05GMT
(s) Sortable: . . . . . . . .2008-11-01T23:39:05
(u) Universal sortable: . . . 2008-11-01 23:39:05Z(invariant)
(U) Universalsortable: . . . 200811115:39:05
(Y) Year: . . . . . .. . . . 200811

Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
    (default):.. . . . . . . Green (default ='G')
(F) Flags:. . . . . . . . . . Green (flags orinteger)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003



C#格式化数值结果表
字符   说明             示例                                 输出
    货币               string.Format("{0:C3}",2)                        $2.000
    十进制             string.Format("{0:D3}",2)             002
    科学计数法         1.20E+001 1.20E+001 G 常规                   string.Format("{0:G}",2) 2
    用分号隔开的数字     string.Format("{0:N}",250000)         250,000.00
    十六进制           string.Format("{0:X000}",12)           C


字符型转换 转为字符串
12345.ToString("n"); //生成 12,345.00
12345.ToString("C"); //生成 ¥12,345.00
12345.ToString("e"); //生成 1.234500e+004
12345.ToString("f4"); //生成 12345.0000
12345.ToString("x"); //生成 3039 (16进制)
12345.ToString("p"); //生成 1,234,500.00%


原文地址http://blog.sina.com.cn/s/blog_52ee6fc30100bgmx.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值