现在的位置: 首页 > 综合 > 正文

C# string.format() 格式化说明符

2013年10月09日 ⁄ 综合 ⁄ 共 7766字 ⁄ 字号 评论关闭
IFormatProvider由 实现。
        NumberFormatInfo提供用于格式化基本数据类型的数字的区域性特定信息,
        DateTimeFormatInfo提供用于格式化日期和时间值的区域性特定信息。
 

NumberFormatInfo 属性:

格式字符

说明和关联属性

c、C

货币格式。 CurrencyNegativePattern, CurrencyPositivePattern, CurrencySymbol, CurrencyGroupSizes, CurrencyGroupSeparator, CurrencyDecimalDigits, CurrencyDecimalSeparator.

d、D

十进制格式。

e、E

科学计数(指数)格式。

f、F

固定点格式。

g、G

常规格式。

n、N

数字格式。 NumberNegativePattern, NumberGroupSizes, NumberGroupSeparator, NumberDecimalDigits, NumberDecimalSeparator.

r、R

往返格式,这确保将已转换成字符串的数字转换回数字时具有与原数字相同的值。

x、X

十六进制格式。

只能为固定区域性或特定区域性创建 DateTimeFormatInfo 或 NumberFormatInfo,而不能为非特定区域性创建它们。有关固定区域性、特定区域性和非特定区域性的更多信息,请参见 CultureInfo 类。

该类实现 接口以启用 NumberFormatInfo 对象的复制。它还实现 以便为应用程序提供格式化信息。

 

using System;
using System.Globalization;
using System.Text;

public sealed class App 
{
    static void Main() 
    {
        StringBuilder sb = new StringBuilder();

        // Loop through all the specific cultures known to the CLR.
        foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures)) 
        {
            // Only show the currency symbols for cultures that speak English.
            if (ci.TwoLetterISOLanguageName != "en") continue;
             
            // Display the culture name and currency symbol.
            NumberFormatInfo nfi = ci.NumberFormat;
            sb.AppendFormat("The currency symbol for '{0}' is '{1}'",
                ci.DisplayName, nfi.CurrencySymbol);
            sb.AppendLine();
        }
        Console.WriteLine(sb.ToString());
    }
}

// This code produces the following output.
//
// The currency symbol for 'English (United States)' is '$'
// The currency symbol for 'English (United Kingdom)' is ''
// The currency symbol for 'English (Australia)' is '$'
// The currency symbol for 'English (Canada)' is '$'
// The currency symbol for 'English (New Zealand)' is '$'
// The currency symbol for 'English (Ireland)' is '?'
// The currency symbol for 'English (South Africa)' is 'R'
// The currency symbol for 'English (Jamaica)' is 'J$'
// The currency symbol for 'English (Caribbean)' is '$'
// The currency symbol for 'English (Belize)' is 'BZ$'
// The currency symbol for 'English (Trinidad and Tobago)' is 'TT$'
// The currency symbol for 'English (Zimbabwe)' is 'Z$'
// The currency symbol for 'English (Republic of the Philippines)' is 'Php'
 
DateTimeFormatInfo 属性:

格式字符

关联属性/说明

d

ShortDatePattern

D

LongDatePattern

f

完整日期和时间(长日期和短时间)

F

FullDateTimePattern(长日期和长时间)

g

常规(短日期和短时间)

G

常规(短日期和长时间)

m、M

MonthDayPattern

r、R

RFC1123Pattern

s

使用当地时间的 SortableDateTimePattern(基于 ISO 8601)

t

ShortTimePattern

T

LongTimePattern

u

UniversalSortableDateTimePattern 用于显示通用时间的格式

U

使用通用时间的完整日期和时间(长日期和长时间)

y、Y

YearMonthPattern

下表列出了可被合并以构造自定义模式的模式。这些模式是区分大小写的;例如,识别“MM”,但不识别“mm”。如果自定义模式包含空白字符或用单引号括起来的字符,则输出字符串页也将包含这些字符。未定义为格式模式的一部分或未定义为格式字符的字符按其原义复制。

格式模式

说明

d、%d

月中的某一天。一位数的日期没有前导零。如果该格式模式没有与其他格式模式组合,则指定“%d”。

dd

月中的某一天。一位数的日期有一个前导零。

ddd

周中某天的缩写名称,在 AbbreviatedDayNames 中定义。

dddd

周中某天的完整名称,在 DayNames 中定义。

M、%M

月份数字。一位数的月份没有前导零。如果该格式模式没有与其他格式模式组合,则指定“%M”。

MM

月份数字。一位数的月份有一个前导零。

MMM

月份的缩写名称,在 AbbreviatedMonthNames 中定义。

MMMM

月份的完整名称,在 MonthNames 中定义。

y、%y

不包含纪元的年份。如果不包含纪元的年份小于 10,则显示不具有前导零的年份。如果该格式模式没有与其他格式模式组合,则指定“%y”。

yy

不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。

yyyy

包括纪元的四位数的年份。

gg

时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。

h、%h

12 小时制的小时。一位数的小时数没有前导零。如果该格式模式没有与其他格式模式组合,则指定“%h”。

hh

12 小时制的小时。一位数的小时数有前导零。

H、%H

24 小时制的小时。一位数的小时数没有前导零。如果该格式模式没有与其他格式模式组合,则指定“%H”。

HH

24 小时制的小时。一位数的小时数有前导零。

m、%m

分钟。一位数的分钟数没有前导零。如果该格式模式没有与其他格式模式组合,则指定“%m”。

mm

分钟。一位数的分钟数有一个前导零。

s、%s

秒。一位数的秒数没有前导零。如果该格式模式没有与其他格式模式组合,则指定“%s”。

ss

秒。一位数的秒数有一个前导零。

f、%f

秒的小数精度为一位。其余数字被截断。如果该格式模式没有与其他格式模式组合,则指定“%f”。

ff

秒的小数精度为两位。其余数字被截断。

fff

秒的小数精度为三位。其余数字被截断。

ffff

秒的小数精度为四位。其余数字被截断。

fffff

秒的小数精度为五位。其余数字被截断。

ffffff

秒的小数精度为六位。其余数字被截断。

fffffff

秒的小数精度为七位。其余数字被截断。

F、%F

显示秒的小数部分的最高有效数字。如果该数字为零,则不显示任何内容。如果该格式模式没有与其他格式模式组合,则指定“%F”。

FF

显示秒的小数部分的两个最高有效数字。但是,不显示尾随的零(两个零数字)。

FFF

显示秒的小数部分的三个最高有效数字。但是,不显示尾随的零(三个零数字)。

FFFF

显示秒的小数部分的四个最高有效数字。但是,不显示尾随的零(四个零数字)。

FFFFF

显示秒的小数部分的五个最高有效数字。但是,不显示尾随的零(五个零数字)。

FFFFFF

显示秒的小数部分的六个最高有效数字。但是,不显示尾随的零(六个零数字)。

FFFFFFF

显示秒的小数部分的七个最高有效数字。但是,不显示尾随的零(七个零数字)。

t、%t

AMDesignatorPMDesignator 中定义的 AM/PM 指示项的第一个字符(如果存在)。如果该格式模式没有与其他格式模式组合,则指定“%t”。

tt

在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项(如果存在)。

z、%z

时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数没有前导零。例如,太平洋标准时间是“-8”。如果该格式模式没有与其他格式模式组合,则指定“%z”。

zz

时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数有前导零。例如,太平洋标准时间是“-08”。

zzz

完整时区偏移量(“+”或“-”后面跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-08:00”。

:

TimeSeparator 中定义的默认时间分隔符。

/

DateSeparator 中定义的默认日期分隔符。

% c

其中 c 是格式模式(如果单独使用)。也就是说,若要单独使用格式模式“d”、“f”、“F”、“h”、“m”、“s”、“t”、“y”、“z”、“H”或“M”,请指定“%d”、“%f”、“%F”、“%h”、“%m”、“%s”、“%t”、“%y”、“%z”、“%H”或“%M”。

如果格式模式与原义字符或其他格式模式合并,则可以省略“%”字符。

/ c

其中 c 是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“//”。

只有上面第二个表中列出的格式模式才能用于创建自定义模式;在第一个表中列出的标准格式字符不能用于创建自定义模式。自定义模式的长度至少为两个字符;例如,

  • DateTime.ToString( "d" ) 返回 DateTime 值;“d”是标准短日期模式。

  • DateTime.ToString( "%d" ) 返回月中的某天;“%d”是自定义模式。

  • DateTime.ToString( "d " ) 返回后面跟有一个空白字符的月中的某天;“d”是自定义模式。

只能为固定区域性或特定区域性创建 DateTimeFormatInfo 或 NumberFormatInfo,而不能为非特定区域性创建它们。有关固定区域性、特定区域性和非特定区域性的更多信息,请参见 CultureInfo 类。

该类实现 接口以启用 DateTimeFormatInfo 对象的复制。它还实现 以便为应用程序提供格式化信息。

 

using System;
using System.Globalization;


public class SamplesDTFI  {

   public static void Main()  {

      // Creates and initializes a DateTimeFormatInfo associated with the en-US culture.
      DateTimeFormatInfo myDTFI = new CultureInfo( "en-US", false ).DateTimeFormat;

      // Creates a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3).
      // The Gregorian calendar is the default calendar for the en-US culture.
      DateTime myDT = new DateTime( 2002, 1, 3 );

      // Displays the format pattern associated with each format character.
      Console.WriteLine( "FORMAT  en-US EXAMPLE" );
      Console.WriteLine( "CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY/n" );
      Console.WriteLine( "  d     {0}", myDT.ToString("d") );
      Console.WriteLine( "        {0} {1}/n", myDTFI.ShortDatePattern, "(ShortDatePattern)" );
      Console.WriteLine( "  D     {0}", myDT.ToString("D") );
      Console.WriteLine( "        {0} {1}/n", myDTFI.LongDatePattern, "(LongDatePattern)" );
      Console.WriteLine( "  f     {0}/n", myDT.ToString("f") );
      Console.WriteLine( "  F     {0}", myDT.ToString("F") );
      Console.WriteLine( "        {0} {1}/n", myDTFI.FullDateTimePattern, "(FullDateTimePattern)" );
      Console.WriteLine( "  g     {0}/n", myDT.ToString("g") );
      Console.WriteLine( "  G     {0}/n", myDT.ToString("G") );
      Console.WriteLine( "  m     {0}", myDT.ToString("m") );
      Console.WriteLine( "        {0} {1}/n", myDTFI.MonthDayPattern, "(MonthDayPattern)" );
      Console.WriteLine( "  M     {0}", myDT.ToString("M") );
      Console.WriteLine( "        {0} {1}/n", myDTFI.MonthDayPattern, "(MonthDayPattern)" );
      Console.WriteLine( "  r     {0}", myDT.ToString("r") );
      Console.WriteLine( "        {0} {1}/n", myDTFI.RFC1123Pattern, "(RFC1123Pattern)" );
      Console.WriteLine( "  R     {0}", myDT.ToString("R") );
      Console.WriteLine( "        {0} {1}/n", myDTFI.RFC1123Pattern, "(RFC1123Pattern)" );
      Console.WriteLine( "  s     {0}", myDT.ToString("s") );
      Console.WriteLine( "        {0} {1}/n", myDTFI.SortableDateTimePattern, "(SortableDateTimePattern)" );
      Console.WriteLine( "  t     {0}", myDT.ToString("t") );
      Console.WriteLine( "        {0} {1}/n", myDTFI.ShortTimePattern, "(ShortTimePattern)" );
      Console.WriteLine( "  T     {0}", myDT.ToString("T") );
      Console.WriteLine( "        {0} {1}/n", myDTFI.LongTimePattern, "(LongTimePattern)" );
      Console.WriteLine( "  u     {0}", myDT.ToString("u") );
      Console.WriteLine( "        {0} {1}/n", myDTFI.UniversalSortableDateTimePattern, "(UniversalSortableDateTimePattern)" );
      Console.WriteLine( "  U     {0}/n", myDT.ToString("U") );
      Console.WriteLine( "  y     {0}", myDT.ToString("y") );
      Console.WriteLine( "        {0} {1}/n", myDTFI.YearMonthPattern, "(YearMonthPattern)" );
      Console.WriteLine( "  Y     {0}", myDT.ToString("Y") );
      Console.WriteLine( "        {0} {1}/n", myDTFI.YearMonthPattern, "(YearMonthPattern)" );

   }

}

/*
This code produces the following output.

FORMAT  en-US EXAMPLE
CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY

  d     1/3/2002
        M/d/yyyy (ShortDatePattern)

  D     Thursday, January 03, 2002
        dddd, MMMM dd, yyyy (LongDatePattern)

  f     Thursday, January 03, 2002 12:00 AM

  F     Thursday, January 03, 2002 12:00:00 AM
        dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)

  g     1/3/2002 12:00 AM

  G     1/3/2002 12:00:00 AM

  m     January 03
        MMMM dd (MonthDayPattern)

  M     January 03
        MMMM dd (MonthDayPattern)

  r     Thu, 03 Jan 2002 00:00:00 GMT
        ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)

  R     Thu, 03 Jan 2002 00:00:00 GMT
        ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)

  s     2002-01-03T00:00:00
        yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)

  t     12:00 AM
        h:mm tt (ShortTimePattern)

  T     12:00:00 AM
        h:mm:ss tt (LongTimePattern)

  u     2002-01-03 00:00:00Z
        yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)

  U     Thursday, January 03, 2002 8:00:00 AM

  y     January, 2002
        MMMM, yyyy (YearMonthPattern)

  Y     January, 2002
        MMMM, yyyy (YearMonthPattern)

*/

抱歉!评论已关闭.