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

http://www.cnblogs.com/zxjyuan/archive/2008/12/08/1350406.html

一、定义
String.Format
是将指定的 String类型的数据中的每个格式项替换为相应对象的值的文本等效项。 
如:
(1)
string p1 = "Jackie";
string p2 = "Aillo";
Response.Write(String.Format("Hello {0}, I'm {1}", p1, p2));
(2)
Response.Write(String.Format("Hello {0}, I'm {1}", "Jackie", "Aillo"));
这二者的效果是一样的。都是将最后面的两项的值分别替换第一项的{0}和{1}。
输出的结果是:Hello Jackie, I'm Aillo
二、String.Format的多格式定义:
      这里所谓的多格式是指一个格式项中可以定义1~3个格式参数,每种格式参数用分号(;)隔开。带2个和3个格式参数的格式项所对应的值必须是数值类型的,这样才能判断是否为负数、正数、零。
带1个格式参数:
//以科学计数法的格式输出
double p1 = 1000000;
Response.Write(String.Format("{0:E2}", p1));
带2个格式参数:
/*当格式项对应的值为非负数,则选择第一种格式;值为负数则选第二种格式*/
double p1 = 10000;
double p2 = -2420.50;
Response.Write(String.Format("{0:#,###0.00;#,###0.000;}<BR>", p1));
Response.Write(String.Format("{0:#,###0.00;#,###0.000;}", p2));
带3个格式参数:
/*当格式项对应的值为正数则选择第一张格式;
负数则为第二中格式;
值等于零则为第三种格式*/
1double p1 = 10000;
double p2 = -2420.50;
double p3 = 0.00;
Response.Write(String.Format("{0:#,###0.00;#,###0.000;#,###0.0000}<BR>", p1));
Response.Write(String.Format("{0:#,###0.00;#,###0.000;#,###0.0000}<BR>", p3));
Response.Write(String.Format("{0:#,###0.00;#,###0.000;#,###0.0000}", p2));
补充:
{0:N2}
 中的N3,f3表示格式化之后数据的类型以及小数的位数。如:N2表示带2个小数的数字;
与此类似:
N或者n  表示  数字
F或者f   表示  固定点
E或者e  表示  科学计数法
D或者d  表示  十进制数
X或者x  表示  十六进制
G或者g  表示  常规
C或者c  表示  货币
 
IFormatProvider 由 NumberFormatInfo 和 DateTimeFormatInfo 实现。NumberFormatInfo 提供用于格式化基本数据类型的数字的区域性特定信息,而 DateTimeFormatInfo 提供用于格式化日期和时间值的区域性特定信息。
 

NumberFormatInfo 属性:

格式字符

说明和关联属性

c、C

货币格式。 CurrencyNegativePatternCurrencyPositivePatternCurrencySymbolCurrencyGroupSizesCurrencyGroupSeparatorCurrencyDecimalDigits,CurrencyDecimalSeparator.

d、D

十进制格式。

e、E

科学计数(指数)格式。

f、F

固定点格式。

g、G

常规格式。

n、N

数字格式。 NumberNegativePatternNumberGroupSizesNumberGroupSeparatorNumberDecimalDigitsNumberDecimalSeparator.

r、R

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

x、X

十六进制格式。

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

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

 

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 类。

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

 

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)
*/


  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值