Convert float to string using the specified format in CSharp

Description

The following code shows how to convert float to string using the specified format.

Example


using System;/*from  w ww . j  a v  a  2 s. c  om*/
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));
        
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var