Mega Code Archive

 
Categories / C# / Data Types
 

Converts float to string using the specified format

using System; using System.Globalization; public class Example {     public static void Main()     {         float[] numbers= { 1234.56789F, -123456789.8765F, 1.23456E21F,                             -1.0573e-05F };         string[] specifiers = { "C", "E", "e", "F", "G", "N", "P",                                  "R", "#,000.000", "0.###E-000",                                 "000,000,000,000.00###" };                  foreach (float number in numbers)         {            foreach (string specifier in specifiers)               Console.WriteLine("   {0,5}: {1}", specifier, number.ToString(specifier));                  }     } } /*        C: $1,234.57        E: 1.234568E+003        e: 1.234568e+003        F: 1234.57        G: 1234.568        N: 1,234.57        P: 123,456.80 %        R: 1234.56787    #,000.000: 1,234.568    0.###E-000: 1.235E003    000,000,000,000.00###: 000,000,001,234.568        C: ($123,456,800.00)        E: -1.234568E+008        e: -1.234568e+008        F: -123456800.00        G: -1.234568E+08        N: -123,456,800.00        P: -12,345,680,000.00 %        R: -123456792    #,000.000: -123,456,800.000    0.###E-000: -1.235E008    000,000,000,000.00###: -000,123,456,800.00        C: $1,234,560,000,000,000,000,000.00        E: 1.234560E+021        e: 1.234560e+021        F: 1234560000000000000000.00        G: 1.23456E+21        N: 1,234,560,000,000,000,000,000.00        P: 123,456,000,000,000,000,000,000.00 %        R: 1.23456E+21    #,000.000: 1,234,560,000,000,000,000,000.000    0.###E-000: 1.235E021    000,000,000,000.00###: 1,234,560,000,000,000,000,000.00        C: $0.00        E: -1.057300E-005        e: -1.057300e-005        F: 0.00        G: -1.0573E-05        N: 0.00        P: 0.00 %        R: -1.0573E-05    #,000.000: 000.000    0.###E-000: -1.057E-005    000,000,000,000.00###: -000,000,000,000.00001 */