论坛首页 Java企业应用论坛

(转)String.Format

浏览 1372 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2007-12-27  
DAO
    string formatString = string.Empty;                      
                                                             
    //一个格式参数                                           
    formatString = "{0:MM/dd/yy hh:mm:ss tt}";               
    DateTime dt = DateTime.Now;                              
    Console.WriteLine(string.Format(formatString,dt));       
                                                             
    double dNumberOne = 200.20;                              
    double dNumberTwo = -150.50;                             
    double dNumberThree = 0.0;                               
    string sToFormat = "hello";                              
                                                             
    //当出现两部分(分号分割)格式化参数时                   
    //第一部分用来格式化所有非负数的值(包含零)             
    //第二部分用来格式化所有负数                             
    formatString ="{0:¥#,###0.00;(¥#,###0.00)}";           
    Console.WriteLine(string.Format(formatString, dNumberOne));
    Console.WriteLine(string.Format(formatString, dNumberTwo));
                                                             
    //当出现三部分(分号分割)格式化参数时                   
    //第一部分用来格式化所有正数的值(不包含零)             
    //第二部分用来格式化所有负数                             
    //第三部分用来格式化值为零的数                           
    formatString = "{0:¥#,###0.00;(¥#,###0.00);零值}";     
    Console.WriteLine(string.Format(formatString, dNumberOne));
    Console.WriteLine(string.Format(formatString, dNumberTwo));
    Console.WriteLine(string.Format(formatString, dNumberThree));
                                                             
    Console.WriteLine(string.Format(formatString, sToFormat));
-------------------------------------------------------------------
string str1 = String.Format("{0:N1}",56789);               //result: 56,789.0
string str2 = String.Format("{0:N2}",56789);               //result: 56,789.00
string str3 = String.Format("{0:N3}",56789);               //result: 56,789.000
string str8 = String.Format("{0:F1}",56789);               //result: 56789.0
string str9 = String.Format("{0:F2}",56789);               //result: 56789.00
string str11 =(56789 / 100.0).ToString("#.##");            //result: 567.89
string str12 =(56789 / 100).ToString("#.##");              //result: 567

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics