zoukankan      html  css  js  c++  java
  • C#的String.Format举例

    转载自http://www.cnblogs.com/linbaoji/archive/2009/12/11/1621923.html

    C#的String.Format举例

    stringstr1 =string.Format("{0:N1}",56789);                //result: 56,789.0
    stringstr2 =string.Format("{0:N2}",56789);                //result: 56,789.00
    stringstr3 =string.Format("{0:N3}",56789);                //result: 56,789.000
    stringstr8 =string.Format("{0:F1}",56789);                //result: 56789.0
    stringstr9 =string.Format("{0:F2}",56789);                //result: 56789.00
    stringstr11 =(56789 / 100.0).ToString("#.##");            //result: 567.89
    stringstr12 =(56789 / 100).ToString("#.##");              //result: 567

    C 或 c
    货币
    Console.Write("{0:C}", 2.5);   //$2.50
    Console.Write("{0:C}", -2.5); //($2.50)

    D 或 d
    十进制数
    Console.Write("{0:D5}", 25);   //00025

    E 或 e
    科学型
    Console.Write("{0:E}", 250000);   //2.500000E+005

    F 或 f
    固定点
    Console.Write("{0:F2}", 25);   //25.00
    Console.Write("{0:F0}", 25);   //25

    G 或 g
    常规
    Console.Write("{0:G}", 2.5);   //2.5

    N 或 n
    数字
    Console.Write("{0:N}", 2500000);   //2,500,000.00

    X 或 x
    十六进制
    Console.Write("{0:X}", 250); 

    /******************************************************************************/
       ASP.NET设置数据格式与String.Format使用总结(引) 
    {0:d} YY-MM-DD

    {0:p} 百分比00.00%

    {0:N2} 12.68

    {0:N0} 13

    {0:c2} $12.68

    {0:d} 3/23/2003

    {0:T} 12:00:00 AM

    {0:男;;女} 

    DataGrid-数据格式设置表达式 

    数据格式设置表达式 

    .NET Framework 格式设置表达式,它在数据显示在列中之前先应用于数据。此表达式由可选静态文本和用以下格式表示的格式说明符组成: 

    {0:format specifier}

    零是参数索引,它指示列中要格式化的数据元素;因此,通常用零来指示第一个(且唯一的)元素。format specifier 前面有一个冒号 (:),它由一个或多个字母组成,指示如何格式化数据。可以使用的格式说明符取决于要格式化的数据类型:日期、数字或其他类型。下表显示了不同数据类型的格式设置表达式的示例。有关格式设置表达式的更多信息,请参见格式化类型。

    格式设置表达式 


    应用于此数据类型 


    说明 


    Price: {0:C}


    numeric/decimal


    显示“Price:”,后跟以货币格式表示的数字。货币格式取决于通过 Page 指令或 Web.config 文件中的区域性属性指定的区域性设置。 


    {0:D4}


    integer(不能和小数一起使用。) 


    在由零填充的四个字符宽的字段中显示整数。 


    {0:N2}%


    numeric


    显示精确到小数点后两位的数字,后跟“%”。 


    {0:000.0}


    numeric/decimal


    四舍五入到小数点后一位的数字。不到三位的数字用零填充。 


    {0:D}


    date/datetime


    长日期格式(“Thursday, August 06, 1996”)。日期格式取决于页或 Web.config 文件的区域性设置。 


    {0:d}


    date/datetime


    短日期格式(“12/31/99”)。 


    {0:yy-MM-dd}


    date/datetime


    用数字的年-月-日表示的日期(96-08-06)。 

    只读 

    当此列处于编辑模式时,该列中的数据是否显示在可编辑的控件中。

    2006-02-22 | asp.net数据格式的Format-- DataFormatString

    我们在呈现数据的时候,不要将未经修饰过的数据呈现给使用者。例如金额一万元,如果我们直接显示「10000」,可能会导致使用者看成一千或十万,造成使用者阅读数据上的困扰。若我们将一万元润饰后输出为「NT$10,000」,不但让使比较好阅读,也会让使用者减少犯错的机会。
    下列画面为润饰过的结果:
    上述数据除了将DataGrid Web 控件以颜色来区隔记录外,最主要将日期、单价以及小计这三个计字段的数据修饰的更容易阅读。要修饰字段的输出,只要设定字段的DataFormatString 属性即可;其使用语法如下:

    DataFormatString="{0:格式字符串}"

    我们知道在DataFormatString 中的 {0} 表示数据本身,而在冒号后面的格式字符串代表所们希望数据显示的格式;另外在指定的格式符号后可以指定小数所要显示的位数。例如原来的数据为「12.34」,若格式设定为 {0:N1},则输出为「12.3」。其常用的数值格式如下表所示:

    格式字符串 资料 结果
    "{0:C}" 12345.6789 $12,345.68
    "{0:C}" -12345.6789 ($12,345.68)
    "{0:D}" 12345 12345
    "{0:D8}" 12345 00012345
    "{0:E}" 12345.6789 1234568E+004
    "{0:E10}" 12345.6789 1.2345678900E+004
    "{0:F}" 12345.6789 12345.68
    "{0:F0}" 12345.6789 12346
    "{0:G}" 12345.6789 12345.6789
    "{0:G7}" 123456789 1.234568E8
    "{0:N}" 12345.6789 12,345.68
    "{0:N4}" 123456789 123,456,789.0000
    "Total: {0:C}" 12345.6789 Total: $12345.68

    其常用的日期格式如下表所示:

    格式 说明 输出格式
    d 精简日期格式 MM/dd/yyyy
    D 详细日期格式 dddd, MMMM dd, yyyy
    f 完整格式 (long date + short time) dddd, MMMM dd, yyyy HH:mm
    F
    完整日期时间格式
    (long date + long time)
    dddd, MMMM dd, yyyy HH:mm:ss
    g 一般格式 (short date + short time) MM/dd/yyyy HH:mm
    G 一般格式 (short date + long time) MM/dd/yyyy HH:mm:ss
    m,M 月日格式 MMMM dd
    s 适中日期时间格式 yyyy-MM-dd HH:mm:ss
    t 精简时间格式 HH:mm
    T 详细时间格式 HH:mm:ss

    string.format格式结果 

    String.Format

    (C) Currency: . . . . . . . . ($123.00)

    (D) Decimal:. . . . . . . . . -123

    (E) Scientific: . . . . . . . -1.234500E+002

    (F) Fixed point:. . . . . . . -123.45

    (G) General:. . . . . . . . . -123

    (N) Number: . . . . . . . . . -123.00

    (P) Percent:. . . . . . . . . -12,345.00 %

    (R) Round-trip: . . . . . . . -123.45

    (X) Hexadecimal:. . . . . . . FFFFFF85

    (d) Short date: . . . . . . . 6/26/2004

    (D) Long date:. . . . . . . . Saturday, June 26, 2004

    (t) Short time: . . . . . . . 8:11 PM

    (T) Long time:. . . . . . . . 8:11:04 PM

    (f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM

    (F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM

    (g) General date/short time:. 6/26/2004 8:11 PM

    (G) General date/long time: . 6/26/2004 8:11:04 PM

    (M) Month:. . . . . . . . . . June 26

    (R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT

    (s) Sortable: . . . . . . . . 2004-06-26T20:11:04

    (u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)

    (U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM

    (Y) Year: . . . . . . . . . . June, 2004

    (G) General:. . . . . . . . . Green

    (F) Flags:. . . . . . . . . . Green (flags or integer)

    (D) Decimal number: . . . . . 3

    (X) Hexadecimal:. . . . . . . 00000003

    说明: 
    String.Format 
    将指定的 String 中的每个格式项替换为相应对象的值的文本等效项。 

    例子: 

    int iVisit = 100; 
    string szName = "Jackfled"; 
    Response.Write(String.Format("您的帐号是:{0} 。访问了 {1} 次.", szName, iVisit));

  • 相关阅读:
    java中&和&&
    java保留字
    最优路径算法合集(附python源码)(原创)
    十大经典排序算法(python实现)(原创)
    电子欠款单设想(原创)
    羽毛球友谊赛规则(附带程序)(原创)
    基于python的opcode优化和模块按需加载机制研究(学习与个人思路)(原创)
    lazy_import源码解析(原创)
    多线程的音频打标记的python实现(原创)
    关于塔防游戏的浅析(原创)
  • 原文地址:https://www.cnblogs.com/liuliu-NoGirl/p/6490910.html
Copyright © 2011-2022 走看看