Mega Code Archive

 
Categories / C# / Data Types
 

Converts double to string using the specified format

using System; using System.Globalization; public class Example {    public static void Main()    {         double[] numbers= {1234.123456789, -987654321.1234, 1.0123E21, -1.123456e-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,5}: {1}",                                  specifier, number.ToString(specifier));                     Console.WriteLine();         }    } } /* Formatting of 1234.123456789:        C: $1,234.12        E: 1.234123E+003        e: 1.234123e+003        F: 1234.12        G: 1234.123456789        N: 1,234.12        P: 123,412.35 %        R: 1234.123456789    #,000.000: 1,234.123    0.###E-000: 1.234E003    000,000,000,000.00###: 000,000,001,234.12346 Formatting of -987654321.1234:        C: ($987,654,321.12)        E: -9.876543E+008        e: -9.876543e+008        F: -987654321.12        G: -987654321.1234        N: -987,654,321.12        P: -98,765,432,112.34 %        R: -987654321.1234    #,000.000: -987,654,321.123    0.###E-000: -9.877E008    000,000,000,000.00###: -000,987,654,321.1234 Formatting of 1.0123E+21:        C: $1,012,300,000,000,000,000,000.00        E: 1.012300E+021        e: 1.012300e+021        F: 1012300000000000000000.00        G: 1.0123E+21        N: 1,012,300,000,000,000,000,000.00        P: 101,230,000,000,000,000,000,000.00 %        R: 1.0123E+21    #,000.000: 1,012,300,000,000,000,000,000.000    0.###E-000: 1.012E021    000,000,000,000.00###: 1,012,300,000,000,000,000,000.00 Formatting of -1.123456E-05:        C: $0.00        E: -1.123456E-005        e: -1.123456e-005        F: 0.00        G: -1.123456E-05        N: 0.00        P: 0.00 %        R: -1.123456E-05    #,000.000: 000.000    0.###E-000: -1.123E-005    000,000,000,000.00###: -000,000,000,000.00001 */