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

80酷酷网    80kuku.com

  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"

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: