Single.ToString Method

July 28, 2014

Converts the numeric value of this instance to its equivalent string representation.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public override string ToString()

Return Value

Type: System.String
The string representation of the value of this instance.

The return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or a string of the form:

[sign]integral-digits[.[fractional-digits]][e[sign]exponential-digits]

Optional elements are framed in square brackets ([ and ]). Elements that contain the term "digits" consist of a series of numeric characters ranging from 0 to 9. The following table describes each element.

Element

Description

sign

A negative sign or positive sign symbol.

integral-digits

A series of digits specifying the integral part of the number. Integral-digits can be absent if there are fractional-digits.

'.'

A culture-specific decimal point symbol.

fractional-digits

A series of digits specifying the fractional part of the number.

'e'

A lowercase character 'e', indicating exponential (scientific) notation.

exponential-digits

A series of digits specifying an exponent.

Some examples of the return value are "100", "-123,456,789", "123.45e+6", "500", "3.1416", "600", "-0.123", and "-Infinity".

This overload of the ToString method implicitly uses the general numeric format specifier ("G") and the NumberFormatInfo object for the current culture.

The following example uses the default Single.ToString method to display the string representations of a number of Single values.


// Example for the Single.ToString( ) methods.
using System;
using System.Globalization;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      float singleValue = 11876.54321F;
      string format = "  {0,-30}{1}";

      outputBlock.Text += String.Format("This example of\n" +
          "  Single.ToString( ), \n" +
          "  Single.ToString( String ),\n" +
          "  Single.ToString( IFormatProvider ), and \n" +
          "  Single.ToString( String, IFormatProvider )\n" +
          "generates the following output when run in the [{0}] " +
          "culture. \nA Single number is formatted with various " +
          "combinations of format \nstrings and IFormatProvider.",
          CultureInfo.CurrentCulture.Name) + "\n";

      // Format the number without and with format strings.
      outputBlock.Text += String.Format("\nIFormatProvider is not " +
          "used; the default culture is [{0}]:",
          CultureInfo.CurrentCulture.Name) + "\n";
      outputBlock.Text += String.Format(format, "No format string:",
          singleValue.ToString()) + "\n";
      outputBlock.Text += String.Format(format, "'N5' format string:",
          singleValue.ToString("N5")) + "\n";
      outputBlock.Text += String.Format(format, "'E' format string:",
          singleValue.ToString("E")) + "\n";
      outputBlock.Text += String.Format(format, "'E5' format string:",
          singleValue.ToString("E5")) + "\n";

      // Create a CultureInfo object for another culture. Use
      // [Dutch - The Netherlands] unless the current culture
      // is Dutch language. In that case use [English - U.S.].
      string cultureName =
          CultureInfo.CurrentCulture.Name.Substring(0, 2) == "nl" ?
              "en-US" : "nl-NL";
      CultureInfo culture = new CultureInfo(cultureName);

      // Use the CultureInfo object for an IFormatProvider.
      outputBlock.Text += String.Format("\nA CultureInfo object " +
          "for [{0}] is used for the IFormatProvider: ",
          cultureName) + "\n";
      outputBlock.Text += String.Format(format, "No format string:",
          singleValue.ToString(culture)) + "\n";
      outputBlock.Text += String.Format(format, "'N5' format string:",
          singleValue.ToString("N5", culture)) + "\n";
      outputBlock.Text += String.Format(format, "'E' format string:",
          singleValue.ToString("E", culture)) + "\n";

      // Get the NumberFormatInfo object from CultureInfo, and
      // then change the digit group size to 2 and the digit
      // separator to '_'.
      NumberFormatInfo numInfo = culture.NumberFormat;
      numInfo.NumberGroupSizes = new int[] { 2 };
      numInfo.NumberGroupSeparator = "_";

      // Use a NumberFormatInfo object for IFormatProvider.
      outputBlock.Text += String.Format("\nA NumberFormatInfo object with digit " +
          "group size = 2 and \ndigit separator = '_' is used " +
          "for the IFormatProvider:") + "\n";
      outputBlock.Text += String.Format(format, "'N' format string:",
          singleValue.ToString("N", culture)) + "\n";
      outputBlock.Text += String.Format(format, "'E' format string:",
          singleValue.ToString("E", culture)) + "\n";
   }
}

/*
This example of
  Single.ToString( ),
  Single.ToString( String ),
  Single.ToString( IFormatProvider ), and
  Single.ToString( String, IFormatProvider )
generates the following output when run in the [en-US] culture.
A Single number is formatted with various combinations of format
strings and IFormatProvider.

IFormatProvider is not used; the default culture is [en-US]:
  No format string:             11876.54
  'N5' format string:           11,876.54000
  'E' format string:            1.187654E+004
  'E5' format string:           1.18765E+004

A CultureInfo object for [nl-NL] is used for the IFormatProvider:
  No format string:             11876,54
  'N5' format string:           11.876,54000
  'E' format string:            1,187654E+004

A NumberFormatInfo object with digit group size = 2 and
digit separator = '_' is used for the IFormatProvider:
  'N' format string:            1_18_76,54
  'E' format string:            1,187654E+004
*/


The following code example illustrates the use of the Parse(String) method along with the ToString() method.


bool done = false;
string inp;
do
{
   outputBlock.Text += "Enter a real number: ";
   inp = Console.ReadLine();
   try
   {
      s = Single.Parse(inp);
      outputBlock.Text += String.Format("You entered {0}.", s.ToString()) + "\n";
      done = true;
   }
   catch (FormatException)
   {
      outputBlock.Text += "You did not enter a number." + "\n";
   }
   catch (Exception e)
   {
      outputBlock.Text += String.Format("An exception occurred while parsing your response: {0}", e.ToString()) + "\n";
   }
} while (!done);


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft