# Single.ToString メソッド

2013/12/12

このインスタンスの数値を、それと等価の文字列形式に変換します。

Namespace:  System
アセンブリ:  mscorlib (mscorlib.dll 内)

## 構文

```public override string ToString()
```

#### 戻り値

このインスタンスの値の文字列形式。

## 解説

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

sign

integral-digits

'.'

カルチャに固有の小数点記号。

fractional-digits

'e'

exponential-digits

この ToString メソッドのオーバーロードでは、一般数値書式指定子 ("G") および現在のカルチャの NumberFormatInfo オブジェクトを暗黙的に使用します。

## 例

```
// 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
*/

```

Parse(String) メソッドと ToString() メソッドを併用するコード例を次に示します。

```
bool done = false;
string inp;
do
{
outputBlock.Text += "Enter a real number: ";
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

サポート: 8.0, 7.1, 7.0

Windows Phone