C # basic: format the identifier
Letter |
Description |
C or C |
Currency format |
D Or d |
Decimal decimal format (a decimal integer. Do not confuse the decimal data type of. net) |
E or E |
Exponent index format |
F or F |
Fixed Point fixed precision format |
G or G |
Common General Format |
N or N |
Use a comma to separate thousands of digits. For example, 1234 will be changed to 1,234. |
P or P |
Percentage percentile format |
R or R |
Round-trip round (used only for floating point numbers) Ensure that a number can be converted back to the same number after being converted into a string |
X or X |
Hex hexadecimal format |
If we use the following expression, let's see what will happen.
Public class formatspecapp
{
Public static void main (string [] ARGs)
{
Int I = 123456;
Console. writeline ("{0: c}", I); // ¥123,456.00
Console. writeline ("{0: d}", I); // 123456
Console. writeline ("{0: e}", I); // 1.234560e + 005
Console. writeline ("{0: f}", I); // 123456.00
Console. writeline ("{0: g}", I); // 123456
Console. writeline ("{0: n}", I); // 123,456.00
Console. writeline ("{0: p}", I); // 12,345,600.00%
Console. writeline ("{0: x}", I); // 1e240
}
}
The precision control mark controls the number of valid numbers or the number of digits in decimal places.
Console. writeline ("{0: C5}", I); // ¥123,456.00000
Console. writeline ("{0: D5}", I); // 123456
Console. writeline ("{0: E5}", I); // 1.23456e + 005
Console. writeline ("{0: F5}", I); // 123456.00000
Console. writeline ("{0: G5}", I); // 1.23456e + 05
Console. writeline ("{0: N5}", I); // 123,456.00000
Console. writeline ("{0: P5}", I); // 12,345,600.00000%
Console. writeline ("{0: X5}", I); // 1e240
The R (circle) format is only valid for floating point numbers. The value is first formatted in a common format. The dual-precision number has 15-bit precision, and the single-precision number has 7-bit precision. If this value can be correctly parsed back to the original number, it will be formatted with a common format character. If it cannot be parsed back, it will use 17-bit precision to format the double-precision number and 9-bit precision.
Number of formatting single precision. Although we can add the number of digits of a valid number to the end of the circle identifier, it will be ignored.
Double D = 1.2345678901234567890;
Console. writeline ("floating-point:/t {0: F16}", d); // 1.2345678901234600
Console. writeline ("roundtrip:/t {0: R16}", d); // 1.2345678901234567
If the standard format identifier does not meet your requirements. You can use a string in graphical format to create custom string output. Graphical formatting uses placeholders to represent the minimum digits,
The maximum number of digits, the positioning symbol, the appearance of the negative sign, and the appearance of other digital symbols. As shown in the following table ..
Symbol |
Name |
Description |
0 |
0 placeholder |
Fill in insufficient digits with 0 |
# |
Digit placeholder |
Use # to replace the actual number of digits |
. |
Decimal point |
|
, |
Thousands Separator |
Use commas (,) to separate 1000 digits. For example, divide 1,000 into digits. |
% |
Percentile |
Display a percentile ID |
E + 0 E-0 E + 0 E-0 |
Exponent symbol |
Format the output with an exponential sign |
/ |
Special Character |
Used for formatting sequences in traditional formats, such as "/N" (new rows) |
'Abc' "ABC" |
Constant string |
Display strings in single or double quotation marks |
; |
Regional Separator |
If the number is formatted as an integer, negative number, or 0, use; to separate |
,. |
Zoom symbol |
Number divided by 1000 |
See the following example:
Double I = 123456.42;
Console. writeline ();
Console. writeline ("{0: 00. 00}", I); // 123456.42
Console. writeline ("{0: 00. 00000000e + 0}", I); // 12.34564200e + 4
Console. writeline ("{0: 0,.}", I); // 123
Console. writeline ("{0: #0.000}", I); // 123456.420
Console. writeline ("{0: #0.000; (#0.000)}", I); // 123456.420
Console. writeline ("{0: #0.000; (#0.000); <zero >}", I); // 123456.420
Console. writeline ("{0 :#%}", I); // 12345642%
I =-123456.42;
Console. writeline ();
Console. writeline ("{0: 00. 00}", I); //-123456.42
Console. writeline ("{0: 00. 00000000e + 0}", I); //-12.34564200e + 4
Console. writeline ("{0: 0,.}", I); //-123
Console. writeline ("{0: #0.000}", I); //-123456.420
Console. writeline ("{0: #0.000; (#0.000)}", I); // (123456.420)
Console. writeline ("{0: #0; (#0); <zero >}", I); // (123456)
Console. writeline ("{0 :#%}", I); //-12345642%
I = 0;
Console. writeline ();
Console. writeline ("{0: 0,.}", I); // 0
Console. writeline ("{0: #0}", I); // 0
Console. writeline ("{0: #0; (#0)}", I); // 0
Console. writeline ("{0: #0; (#0); <zero >}", I); // <zero>
Console. writeline ("{0 :#%}", I); // % No tag for this post.