新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > .Net开发 > Asp.net教程 > 正文:Picture Numeric Format Strings(我很难解释大家自己看)

Picture Numeric Format Strings(我很难解释大家自己看)

新客网 XKER.COM 2003-07-12 来源: 收藏本文
http://msdn.microsoft.com/library/dotnet/cpguide/cpconpicturenumericformatstrings.htm(直接的连接url)
If the built-in formatting strings do not provide the type of formatting you require, you can use picture format strings to create custom string output. Picture format strings contain a number of format characters that arrange the output of the Format method in almost any way imaginable.

Picture number format definitions are described using placeholder strings that identify the minimum and maximum number of digits used, the placement or appearance of the negative sign, and the appearance of any other text within the number.

The following table lists the picture formatting characters.


Format character Description Description
0 Display zero placeholder Results in a non-significant zero if a number has fewer digits than there are zeros in the format.
# Display digit placeholder Replaces the "#" symbol with only significant digits.
. Decimal point Displays a "." character.
, Group separator
multiplier
Separates number groups; for example, "1,000".
% Percent notation Displays a "%" character.
E+0
E-0

e+0

e-0
Exponent notation Formats the output of exponent notation.
\ Literal character Used with traditional formatting sequences like "\n" (newline).  
'ABC'
"ABC"
Literal string Displays any string within quotes or apostrophes literally.
; Section separator Specifies different output if the numeric value to be formatted is positive, negative, or zero.


The following code is an example of picture number formatting.

[C#]
Double MyDouble = 12.345;

MyDouble. Format( "000.##", null );
// returns the string "012.35"

Double MyDouble = 5.14129
Int32  MyInt = 42

MyDouble.Format( "0###.##", null )
//Results in "0005.14"

MyDouble.Format( "%#.##", null )
//Results in "%514.13"

MyInt.Format( "My Number = #", null )
//Results in "My Number = 42"

MyInt.Format( "My Number \n= #", null )
//Results in "My Number
//             = 42"
Picture formatting strings can be broken into three sections. If only one section is defined, all numbers will be formatted as specified in that section. If two sections are defined, the first section will be applied to positive and zero values while the second section will be applied to negative numbers. If three sections are defined, the first applies to positive values, the second applies to negative values, and the third applies to values of zero. For example:

[C#]
int MyPosInt = 42, MyNegInt = -42, MyZeroInt = 0;

MyPosInt.Format( "Positive Number = #; Negative Number = #; Zero Value = #", null )

//Results in "Positive Number = 42"

MyNegInt.Format( "Positive Number = #; Negative Number = #; Zero Value = #", null )

//Results in "Negative Number =  -42"

MyZeroInt.Format( "Positive Number = #; Negative Number = #; Zero Value = #", null )

//Results in "Zero Value = 0"
收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐