新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > .Net开发 > Asp.net教程 > 正文:数值变换时的格式化字符举例

数值变换时的格式化字符举例

新客网 XKER.COM 2003-07-12 来源: 收藏本文
http://msdn.microsoft.com/library/dotnet/cpguide/cpconstandardnumericformatstrings.htm(这个是连接url)
Standard Numeric Format Strings are used to return commonly used numeric string types. They take the form X0, where X is the format specifier and 0 is the precision specifier. The format specifier can be one of the seven built-in format characters that define the most commonly used .NET Framework numeric format types. The precision specifier controls the number of significant digits or zeros to the right of a decimal.


Format character Description Default return format (without precision specifier)
C or c Currency format $XX,XX.XX
($XX,XXX.XX)

D or d Decimal format [-]XXXXXXX
E or e Scientific (exponential) format [-]X.XXXXXXE+xxx
[-]X.XXXXXXe+xxx

[-]X.XXXXXXE-xxx

[-]X.XXXXXXe-xxx

F or f Fixed-point format [-]XXXXXXX.XX
G or g General format Variable. Either general or scientific.
N or n Number format [-]XX,XXX.XX
X or x Hexadecimal format Variable. Returns the minimum hexadecimal representation.  


Currency Format
The "C" format specifier causes the Format method to return a string representing the number as a currency value. The currency symbols used (currency symbol, decimal separator, group separator, and so on) are determined by the current culture if a NumberFormatInfo object is not provided. An integer following the "C" determines the number of decimal places that are displayed. If no number is provided, two digits are shown after the decimal separator.

[C#]
int MyInt = 12345;

MyInt.format( "c", null );
// Returns the currency string "$12,345.00"

MyInt.format( "c3", null );
// Returns the exponential string "$12,345.000"
Decimal Format
The "D" format specifier converts the value from an integer to a base 10 (decimal) number. Only integral types support the "D" format code. A negative symbol is prefixed to the result if the value is negative. You can also specify the minimum number of decimal digits to display using a precision specifier.

[C#]
int MyInt = 123;

MyInt.format( "d5", null );
// Returns the decimal string "00123"

MyInt.format( "d2", null );
// Returns the decimal string "123"
Exponential Format
The “E” format specifier causes the Format method to return a string formatted as a scientific (or exponential) number. The precision specifier determines the number of digits after the decimal point. The case of the exponent format character ("E" or "e") determines the case of the exponent symbol.

[C#]
int MyInt = 12345;

MyInt.format( "e", null );
// Returns the exponential string "1.234500e+004"

MyInt.format( "e3", null );
// Returns the exponential string "1.235e+004"

Fixed Point Format
The "F" format specifier inserts a decimal to the right of a nondecimal number followed by the number of zeros specified by the precision specifier. If no precision specifier is supplied, two zeros will be inserted.

[C#]
int MyInt = 12345;

MyInt.format( "f", null );
// Returns the exponential string "12345.00"

MyInt.format( "f3", null );
// Returns the exponential string "12345.000"
General Format
The "G" format specifier converts a numeric value to either fixed point ("F") or scientific format ("E"). This specifier returns the most compact string representation for a given number.

[C#]
int MyInt = 12345;

MyInt.format( "g", null );
// Returns the exponential string "12345"

MyInt.format( "g3", null );
// Returns the exponential string "123e4"
Number Format
The "N" format specifier converts a numeric value to the form "[-]d,ddd,ddd.dd". A decimal is inserted at the far right of the number followed by the number of zeros specified by the format specifier. If no precision specifier is supplied, two zeros are inserted.

[C#]
int MyInt = 12345;

MyInt.format( "n", null );
// Returns the exponential string "12,345.00"

MyInt.format( "n3", null );
// Returns the exponential string "12,345.000"
Hexadecimal Format
The "X" format specifier converts a numeric value to a hexadecimal (base 16) string representation. The precision specifier determines the minimum number of digits returned. If no precision specifier is supplied, the minimum number of digits needed to represent the value is returned; otherwise, the number will be padded with zeros to meet the number of digits required by the precision specifier.

[C#]
int MyInt = 12345;

MyInt.format( "x", null );
// Returns the exponential string "3039"

MyInt.format( "x3", null );
// Returns the exponential string "3039"
收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐