ToString () formatted data in C #

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,00   0.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.for            Mat ("{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:1                     054.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////Formatti ng of-195489100.8377://C: ($195,489,100.84)//C0: ($195,489,101        )//C1:            ($195,489,100.8)//C2: ($195,489,100.84)//C3: ($195,4          89,100.838)////E: -1.954891e+008//E0: -2e+008// E1: -2.0e+008//E2: -1.95e+008//E3: -1.955E+0                    08////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,91 0,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////Form                    Atting 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,0                     00,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//E 2: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:1043700000000000 000000.00//f0:1043700000000000000000//f1:104370000000000000000 0.0//f2:1043700000000000000000.00//f3:1043700000000000000000.0 00////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,0 00,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.00 0:1,043,700, 000,000,000,000,000.000//0.## #E -000:1.044e021//000,000,000,000.00###: 1,043,700,000,000,0                    00,000,000.00////Formatting of-1.0573e-05://C: $0.00//C0:                    $0//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-00                    5////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,00  0,000.00###: -000,000,000,000.00001


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

ToString () formatted data in C #

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.