C# Single ToString(String)

Description

Single ToString(String) converts the numeric value of this instance to its equivalent string representation, using the specified format.

Syntax

Single.ToString(String) has the following syntax.


public string ToString(
  string format
)

Parameters

Single.ToString(String) has the following parameters.

  • format - A numeric format string.

Returns

Single.ToString(String) method returns The string representation of the value of this instance as specified by format.

Example

The following example displays several Single values using each of the supported standard numeric format specifiers together with two custom numeric format strings.


//from  ww w. j  a  v  a 2  s .  co m
using System;
public class MainClass{
  public static void Main(String[] argv){  
    float[] numbers= { 1234.12345F, -123456789.1234F, 1.1234E21F, 
                       -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)
    {
       Console.WriteLine("Formatting of {0}:", number);
       foreach (string specifier in specifiers)
          Console.WriteLine("   {0,5}: {1}", 
                            specifier, number.ToString(specifier));
    
    }
  }
}
    

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version