Examples of formatting characters for numeric transformations

Source: Internet
Author: User
Tags numeric numeric value
Http://msdn.microsoft.com/library/dotnet/cpguide/cpconstandardnumericformatstrings.htm (This is the connection 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 is one of the seven built-in format characters that define the most commonly. NET Framework numeric format types. The precision specifier controls the number of significant digits or zeros to the right of 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 C Ulture 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 ' to a base (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 is 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 b Y the precision specifier. If no precision specifier is supplied, two zeros'll 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.

[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) 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 is 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"


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.