首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
Windows程序
> 详细
【转载C#的String.Format举例 】
时间:
2015-05-14 15:51:52
阅读:
168
评论:
0
收藏:
0
[点我收藏+]
标签:
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));
【转载C#的String.Format举例 】
标签:
原文地址:http://www.cnblogs.com/experience/p/4503100.html
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
动态 WebApi 引擎使用教程(3行代码完成动态 WebApi 构建)
2021-07-28
windows 查看文件的md5/sha1/sha256
2021-07-28
git windows下换行符问题 LF与CRLF转换
2021-07-27
FileExistsError: [WinError 183] 当文件已存在时,无法创建该文件。
2021-07-26
K8S--可视化界面Kubernetes Dashboard(API Server方式)
2021-07-26
Redis安装成windows服务
2021-07-26
c#32位支持大内存(>2gb)
2021-07-23
【c#】Dev BarStaticItem问题汇总
2021-07-23
Exception: URL fetch failure on https://s3.amazonaws.com/text-datasets/nietzsche.txt: None -- [WinError 10054] 远程主机强迫关闭了一个现有的连接。
2021-07-22
WinForm使用DataGridView实现类似Excel表格的查找替换
2021-07-22
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!