技术文章 > 数值变换时的格式化字符举例

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

2018-08-18 23:46

文档管理软件,文档管理系统,知识管理系统,档案管理系统的技术资料:


日期:2005-6-12 12:16:00 来源: 编辑: 17     [全屏查看全文]

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“