C # ToString () data format

Source: Internet
Author: User

Double[] numbers= {1054.32179, -195489100.8377, 1.0437E21,
-1.0573E-05};
String[] Specifiers = {"C", "E", "E", "F", "G", "N", "P",
"R", "#,000.000", "0.## #E-000",
"000,000,000,000.00###"};
foreach (double number in numbers)
{
Console.WriteLine ("Formatting of {0}:", number);
foreach (string specifier in specifiers) {
Console.WriteLine ("{0,-22} {1}",
Specifier + ":", number. ToString (specifier));
ADD precision specifiers from 0 to 3.
if (specifier. Length = = 1 &! Specifier. Equals ("R")) {
for (int precision = 0; precision <= 3; precision++) {
String pspecifier = String.Format ("{0}{1}", specifier, precision);
Console.WriteLine ("{0,-22} {1}",
Pspecifier + ":", number. ToString (Pspecifier));
}
Console.WriteLine ();
}
}
Console.WriteLine ();
}
The example displays the following output to the console:
Formatting of 1054.32179:
C: $1,054.32
C0: $1,054
C1: $1,054.3
C2: $1,054.32
C3: $1,054.322
//
e:1.054322e+003
e0:1e+003
e1:1.1e+003
e2:1.05e+003
e3:1.054e+003
//
e:1.054322e+003
e0:1e+003
e1:1.1e+003
e2:1.05e+003
e3:1.054e+003
//
f:1054.32
f0:1054
f1:1054.3
f2:1054.32
f3:1054.322
//
g:1054.32179
g0:1054.32179
g1:1e+03
g2:1.1e+03
g3:1.05e+03
//
n:1,054.32
n0:1,054
n1:1,054.3
n2:1,054.32
n3:1,054.322
//
p:105,432.18%
p0:105,432%
p1:105,432.2%
p2:105,432.18%
p3:105,432.179%
//
r:1054.32179
#,000.000:1,054.322
0.## #E -000:1.054e003
000,000,000,000.00###: 000,000,001,054.32179
//
Formatting of-195489100.8377:
C: ($195,489,100.84)
C0: ($195,489,101)
C1: ($195,489,100.8)
C2: ($195,489,100.84)
C3: ($195,489,100.838)
//
E: -1.954891E+008
E0: -2E+008
E1: -2.0E+008
E2: -1.95E+008
E3: -1.955E+008
//
E: -1.954891e+008
E0: -2e+008
E1: -2.0e+008
E2: -1.95e+008
E3: -1.955e+008
//
F:-195489100.84
F0:-195489101
F1:-195489100.8
F2:-195489100.84
F3:-195489100.838
//
G:-195489100.8377
G0:-195489100.8377
G1: -2E+08
G2: -2E+08
G3: -1.95E+08
//
N: -195,489,100.84
N0:-195,489,101
N1: -195,489,100.8
N2: -195,489,100.84
N3: -195,489,100.838
//
P: -19,548,910,083.77
P0:-19,548,910,084%
P1: -19,548,910,083.8
P2: -19,548,910,083.77
P3: -19,548,910,083.770
//
R:-195489100.8377
#,000.000: -195,489,100.838
0.## #E -000: -1.955E008
000,000,000,000.00###: -000,195,489,100.8377
//
Formatting of 1.0437E+21:
C: $1,043,700,000,000,000,000,000.00
C0: $1,043,700,000,000,000,000,000
C1: $1,043,700,000,000,000,000,000.0
C2: $1,043,700,000,000,000,000,000.00
C3: $1,043,700,000,000,000,000,000.000
//
e:1.043700e+021
e0:1e+021
e1:1.0e+021
e2:1.04e+021
e3:1.044e+021
//
e:1.043700e+021
e0:1e+021
e1:1.0e+021
e2:1.04e+021
e3:1.044e+021
//
f:1043700000000000000000.00
f0:1043700000000000000000
f1:1043700000000000000000.0
f2:1043700000000000000000.00
f3:1043700000000000000000.000
//
G:1.0437e+21
G0:1.0437e+21
G1:1e+21
G2:1e+21
G3:1.04e+21
//
n:1,043,700,000,000,000,000,000.00
n0:1,043,700,000,000,000,000,000
n1:1,043,700,000,000,000,000,000.0
n2:1,043,700,000,000,000,000,000.00
n3:1,043,700,000,000,000,000,000.000
//
p:104,370,000,000,000,000,000,000.00%
p0:104,370,000,000,000,000,000,000%
p1:104,370,000,000,000,000,000,000.0%
p2:104,370,000,000,000,000,000,000.00%
p3:104,370,000,000,000,000,000,000.000%
//
R:1.0437e+21
#,000.000:1,043,700,000,000,000,000,000.000
0.## #E -000:1.044e021
000,000,000,000.00###: 1,043,700,000,000,000,000,000.00
//
Formatting of-1.0573e-05:
C: $0.00
C0: $
C1: $0.0
C2: $0.00
C3: $0.000
//
E: -1.057300E-005
E0: -1E-005
E1: -1.1E-005
E2: -1.06E-005
E3: -1.057E-005
//
E: -1.057300e-005
E0: -1e-005
E1: -1.1e-005
E2: -1.06e-005
E3: -1.057e-005
//
f:0.00
f0:0
f1:0.0
f2:0.00
f3:0.000
//
G: -1.0573E-05
G0: -1.0573E-05
G1: -1E-05
G2: -1.1E-05
G3: -1.06E-05
//
n:0.00
n0:0
n1:0.0
n2:0.00
n3:0.000
//
p:0.00%
P0:0%
p1:0.0%
p2:0.00%
P3:-0.001%
//
R: -1.0573E-05
#,000.000:000.000
0.## #E -000: -1.057E-005
000,000,000,000.00###: -000,000,000,000.00001

C # ToString () data format

Related Article

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.