格式项都采用如下形式:
{index[,alignment][:formatString]}
其中"index"指索引占位符,这个肯定都知道;
",alignment"按字面意思显然是对齐方式,以","为标记;
":formatString"就是对输出格式的限定,以":"为标记。
alignment:可选,是一个带符号的整数,指示首选的格式化字段宽度。如果“对齐”值小于格式化字符串的长度,“对齐”会被忽略,并且使用格式化字符串的长度作为字段宽度。如果“对齐”为正数,字段的格式化数据为右对齐;如果“对齐”为负数,字段的格式化数据为左对齐。如果需要填充,则使用空白。如果指定“对齐”,就需要使用逗号。
formatString:由标准或自定义格式说明符组成.
下表是从网上得来:
字符 | 说明 | 示例 | 输出 |
C | 货币 | string.Format("{0:C3}", 2) | $2.000 |
D | 十进制 | string.Format("{0:D3}", 2) | 002 |
E | 科学计数法 | 1.20E+001 | 1.20E+001 |
G | 常规 | string.Format("{0:G}", 2) | 2 |
N | 用分号隔开的数字 | string.Format("{0:N}", 250000) | 250,000.00 |
X | 十六进制 | string.Format("{0:X000}", 12) | C |
|
| string.Format("{0:000.000}", 12.2) | 012.200 |
Specifier | Type | Format | Output | Output |
c | Currency | {0:c} | $1.42 | -$12,400 |
d | Decimal (Whole number) | {0:d} | System. | -12400 |
e | Scientific | {0:e} | 1.420000e+000 | -1.240000e+004 |
f | Fixed point | {0:f} | 1.42 | -12400.00 |
g | General | {0:g} | 1.42 | -12400 |
n | Number with commas for thousands | {0:n} | 1.42 | -12,400 |
r | Round trippable | {0:r} | 1.42 | System. |
x | Hexadecimal | {0:x4} | System. | cf90 |
Specifier | Type | Example (Passed System.DateTime.Now) |
d | Short date | 10/12/2002 |
D | Long date | December 10, 2002 |
t | Short time | 10:11 PM |
T | Long time | 10:11:29 PM |
f | Full date & time | December 10, 2002 10:11 PM |
F | Full date & time (long) | December 10, 2002 10:11:29 PM |
g | Default date & time | 10/12/2002 10:11 PM |
G | Default date & time (long) | 10/12/2002 10:11:29 PM |
M | Month day pattern | December 10 |
r | RFC1123 date string | Tue, 10 Dec 2002 22:11:29 GMT |
s | Sortable date string | 2002-12-10T22:11:29 |
u | Universal sortable, local time | 2002-12-10 22:13:50Z |
U | Universal sortable, GMT | December 11, 2002 3:13:50 AM |
Y | Year month pattern | December, 2002 |
Specifier | Type | Example | Example Output |
dd | Day | {0:dd} | 10 |
ddd | Day name | {0:ddd} | Tue |
dddd | Full day name | {0:dddd} | Tuesday |
f, ff, ... | Second fractions | {0:fff} | 932 |
gg, ... | Era | {0:gg} | A.D. |
hh | 2 digit hour | {0:hh} | 10 |
HH | 2 digit hour, 24hr format | {0:HH} | 22 |
mm | Minute 00-59 | {0:mm} | 38 |
MM | Month 01-12 | {0:MM} | 12 |
MMM | Month abbreviation | {0:MMM} | Dec |
MMMM | Full month name | {0:MMMM} | December |
ss | Seconds 00-59 | {0:ss} | 46 |
tt | AM or PM | {0:tt} | PM |
yy | Year, 2 digits | {0:yy} | 02 |
yyyy | Year | {0:yyyy} | 2002 |
zz | Timezone offset, 2 digits | {0:zz} | -05 |
zzz | Full timezone offset | {0:zzz} | -05:00 |
: | Separator | {0:hh:mm:ss} | 10:43:20 |
/ | Separator | {0:dd/MM/yyyy} | 10/12/2002 |
示例:
// Console.WriteLine 中各种数据格式的输出
Console.WriteLine("{0, 8 :C}", 2); // $2.00
Console.WriteLine("{0, 8 :C3}", 2); // $2.000
Console.WriteLine("{0 :D3}", 2); // 002
Console.WriteLine("{0 :E}", 2); // 2.000000E+000
Console.WriteLine("{0 :G}", 2); // 2
Console.WriteLine("{0 :N}", 2500000.00); // 2,500,00.00
Console.WriteLine("{0 :x4}", 12); // 000c
Console.WriteLine("{0, 2 :x}", 12); // c
Console.WriteLine("{0 :000.000}", 12.23); // 012.230
Console.WriteLine("{0 :r}", 15.62); // 15.62
Console.WriteLine("{0 :d}", System.DateTime.Now); // 2012-3-27
Console.WriteLine("{0 :D}", System.DateTime.Now); // 2012年3月27日
Console.WriteLine("{0 :t}", System.DateTime.Now); // 11:43
Console.WriteLine("{0 :T}", System.DateTime.Now); // 11:43:34
Console.WriteLine("{0 :f}", System.DateTime.Now); // 2012年3月27日 11:43
Console.WriteLine("{0 :F}", System.DateTime.Now); // 2012年3月27日 11:43:34
Console.WriteLine("{0 :g}", System.DateTime.Now); // 2012-3-27 11:43
Console.WriteLine("{0 :G}", System.DateTime.Now); // 2012-3-27 11:43:34
Console.WriteLine("{0 :M}", System.DateTime.Now); // 3月27日
Console.WriteLine("{0 :r}", System.DateTime.Now);// Tue, 27 Mar 2012 11:43:34 GMT
Console.WriteLine("{0 :s}", System.DateTime.Now); // 2012-03-27T11:43:34
Console.WriteLine("{0 :u}", System.DateTime.Now); // 2012-03-27 11:43:34Z
Console.WriteLine("{0 :U}", System.DateTime.Now); // 2012年3月27日 3:43:34
Console.WriteLine("{0 :Y}", System.DateTime.Now); // 2012年3月
Console.WriteLine("{0 :dd}", System.DateTime.Now); // 27
Console.WriteLine("{0 :ddd}", System.DateTime.Now); // 二
Console.WriteLine("{0 :dddd}", System.DateTime.Now); // 星期二
Console.WriteLine("{0 :f}", System.DateTime.Now); // 2012年3月27日 11:46
Console.WriteLine("{0 :ff}", System.DateTime.Now); // 18
Console.WriteLine("{0 :fff}", System.DateTime.Now); // 187
Console.WriteLine("{0 :ffff}", System.DateTime.Now); // 1875
Console.WriteLine("{0 :fffff}", System.DateTime.Now); // 18750
Console.WriteLine("{0 :gg}", System.DateTime.Now); // 公元
Console.WriteLine("{0 :ggg}", System.DateTime.Now); // 公元
Console.WriteLine("{0 :gggg}", System.DateTime.Now); // 公元
Console.WriteLine("{0 :ggggg}", System.DateTime.Now); // 公元
Console.WriteLine("{0 :gggggg}", System.DateTime.Now); // 公元
Console.WriteLine("{0 :hh}", System.DateTime.Now); // 11
Console.WriteLine("{0 :HH}", System.DateTime.Now); // 11
Console.WriteLine("{0 :mm}", System.DateTime.Now); // 50
Console.WriteLine("{0 :MM}", System.DateTime.Now); // 03
Console.WriteLine("{0 :MMM}", System.DateTime.Now); // 三月
Console.WriteLine("{0 :MMMM}", System.DateTime.Now); // 三月
Console.WriteLine("{0 :ss}", System.DateTime.Now); // 43
Console.WriteLine("{0 :tt}", System.DateTime.Now); // 上午
Console.WriteLine("{0 :yy}", System.DateTime.Now); // 12
Console.WriteLine("{0 :yyyy}", System.DateTime.Now); // 2012
Console.WriteLine("{0 :zz}", System.DateTime.Now); // +08
Console.WriteLine("{0 :zzz}", System.DateTime.Now); // +08:00
Console.WriteLine("{0 :hh:mm:ss}", System.DateTime.Now); // 11:43:34
Console.WriteLine("{0 :dd/MM/yyyy}", System.DateTime.Now); // 27-03-2012
Console.WriteLine()格式化输出小记
控制台I/O显示格式化的结果(转)http://blog.sina.com.cn/s/blog_66770c850100xgko.html
(1)格式字符串(不考虑大小写,除了e/E)
C:货币格式 C2:货币格式,精度为两位小数。 eg:$73.23
D:十进制格式 E:科学计数法
System.Console.Write("{0,5:D2}", i);表示宽度为5,精度为2,不足补0。
D表示是整数,其它的标准数字格式字符串有:
C 本地货币格式
E 科学记数法(指数)格式
F 定点(小数)格式
G 常规格式
N 数字格式
P 百分数格式
X 十六进制格式
R 往返过程
还有一种方式是使用占位符:
double a = 1.2345;
System.Console.WriteLine("{0:###.000000}", a);
结果为1.234500
“#”号位置上有字符就输出,没有则不输出,0的位置上有字符就输出,没有就填0。
再来看个例子: 货币格式
decimal m = 168.24m;
decimal n = 45.8m;
System.Console.WriteLine("{0,8:C2} {1,8:C2} {2,8:C2}", m, n,m-n);
输出结果为
¥168.24
¥45.80
¥122.44
前面有一个空格,因为宽度是8,小数点后保留两位小数,不足补0。
它是右对齐的,我们可以换成左对齐:
¥168.24
¥45.80
¥122.44
“¥”符号是自动加上去的,我们这里选择的是货币格式,它会自动选择适当的符号,RMB当然是¥,要修改可以去控制面板里面设置语言和货币。
以上是数字的格式,另外日期和时间格式字符串也是比较常用的。
static void Main(string[] args)
{
DateTime date1 = new DateTime(2010, 5, 22,19,50,28); //2010年5月22日19点50分28秒
Console.WriteLine(date1.ToString("f",CultureInfo.CreateSpecificCulture("zh-CN")));
}
D 长日期模式 2010年5月22日
f 完整日期/时间模式(短时间) 2010年5月22日 19:50
F 完整日期/时间模式(长时间) 2010年5月22日 19:50:28
g 常规日期/时间模式(短时间) 2010/5/22 19:50
G 常规日期/时间模式(长时间) 2010/5/22 19:50:28
M或m 月日模式 5月22日
t 短时间模式 19:50
T 长时间模式 19:50:28
u 通用的可排序日期/时间模式 2010-05-22 19:50:28Z
U 通用完整日期/时间模式 2010年5月22日 11:50:28
Y或y 年月模式 2010年5月
还可以自定义格式,
{
DateTime date1 = new DateTime(2010, 5, 22,19,50,28);
Console.WriteLine(date1.ToString("yyyy年MM月dd日 tt hh:mm:ss.FF",CultureInfo.CreateSpecificCulture("zh-CN")));
}
输出为2010年05月22日 下午 07:50:28
(2)输出写法
{索引,宽度:格式}
宽度:正值右对齐,负值左对齐
通常为:{索引},{索引:格式},{索引,宽度,格式}
(3)以下代码已编译通过:
using System;
namespace NS
{
class CA
{
public static void Main()
{
decimal i = 940.23m;
decimal j = 73.70m;
Console.WriteLine("{0,9:C2}\n+{1,8:C2}\n----------\n{2,9:C2}",i,j,i+j);
}
}
}
调整 console.write 输出字符的长度
用string.Format或者Console.Write方法的格式化:
{序号索引,M}……
M>0:左对齐(右边留出空格)。
M<0:右对齐(左边留出空格)。
如:
class Program
{
static void Main(string[] args)
{
//行
for (int row = 1; row < 10; row++)
{
//列
for (int col = 1; col <=row; col++)
{
Console.Write("{0}*{1}={2,-5}", row, col, row * col);
}
Console.WriteLine();
}
}
}
Console.WriteLine()函数中{}输出格式详解(C#)
转http://blog.youkuaiyun.com/xrongzhen/article/details/5477075
Console.WriteLine()函数的格式一直没怎么注意。今天同事问起Console.WriteLine({0:D3},a)的意义,忽然发现不知道D代表什么意义。以前以为{0,4}是指第一个变量输出时占8位,今天查了一下,发现也并不完全正确。
其中格式项都采用如下形式:
{index[,alignment][:formatString]}
其中"index"指索引占位符,这个肯定都知道;
",alignment"按字面意思显然是对齐方式,以","为标记;
":formatString"就是对输出格式的限定,以":"为标记。
alignment:可选,是一个带符号的整数,指示首选的格式化字段宽度。如果“对齐”值小于格式化字符串的长度,“对齐”会被忽略,并且使用格式化字符串的长度作为字段宽度。如果“对齐”为正数,字段的格式化数据为右对齐;如果“对齐”为负数,字段的格式化数据为左对齐。如果需要填充,则使用空白。如果指定“对齐”,就需要使用逗号。
formatString:由标准或自定义格式说明符组成.
C#字符输出格式控制
转http://cancait.blog.163.com/blog/static/21335744200711933345140/
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));