次の方法で共有


Convert.ToSingle メソッド (String)

指定した String 形式の数値を、それと等価な単精度浮動小数点数に変換します。

Overloads Public Shared Function ToSingle( _
   ByVal value As String _) As Single
[C#]
public static float ToSingle(stringvalue);
[C++]
public: static float ToSingle(String* value);
[JScript]
public static function ToSingle(
   value : String) : float;

パラメータ

  • value
    変換する数値を格納している String

戻り値

value の値と等価な単精度浮動小数点数。

または

value が null 参照 (Visual Basic では Nothing) の場合は 0。

例外

例外の種類 条件
ArgumentException value が null 参照 (Visual Basic では Nothing) です。
FormatException value の構成が、省略可能な符号と、それに続く 0 から 9 までの一連の数字ではありません。
OverflowException value が MinValue 未満の数値か、 MaxValue より大きい数値を表しています。

解説

戻り値は、value に対して Single.Parse メソッドを呼び出した結果得られる値です。

使用例

[Visual Basic, C#, C++] ToSingle を使用して、 StringSingle に変換する方法については、次のコード例を参照してください。

 
Public Sub ConvertStringFloat(ByVal stringVal As String)
    Dim singleVal As Single = 0

    Try
        singleVal = System.Convert.ToSingle(singleVal)
        System.Console.WriteLine("The string as a single is {0}.", _
                                  singleVal)
    Catch exception As System.OverflowException
        System.Console.WriteLine( _
            "Overflow in string-to-single conversion.")
    Catch exception As System.FormatException
        System.Console.WriteLine( _
            "The string is not formatted as a Single.")
    Catch exception As System.ArgumentException
        System.Console.WriteLine("The string is null.")
    End Try

    ' Single to string conversion will not overflow.
    stringVal = System.Convert.ToString(singleVal)
    System.Console.WriteLine("The single as a string is {0}.", _
                              stringVal)
End Sub

[C#] 
public void ConvertStringFloat(string stringVal) {
    float floatVal = 0;
    
    try {
        floatVal = System.Convert.ToSingle(stringVal);
        System.Console.WriteLine(
            "The string as a float is {0}.", floatVal);
    } 
    catch (System.OverflowException){
        System.Console.WriteLine(
            "The conversion from string-to-float overflowed.");
    }
    catch (System.FormatException) {
        System.Console.WriteLine(
            "The string is not formatted as a float.");
    }
    catch (System.ArgumentNullException) {
        System.Console.WriteLine(
            "The string is null.");
    }

    // Float to string conversion will not overflow.
    stringVal = System.Convert.ToString(floatVal);
    System.Console.WriteLine(
        "The float as a string is {0}.", stringVal);
}

[C++] 

   void ConvertStringFloat(String* stringVal)
   {
      float floatVal = 0;

      try {
         floatVal = System::Convert::ToSingle(stringVal);
         System::Console::WriteLine(S"The String* as a float is {0}.", __box(floatVal));
      } catch (System::OverflowException*) {
         System::Console::WriteLine(S"The conversion from String*-to-float overflowed.");
      } catch (System::FormatException*) {
         System::Console::WriteLine(S"The String* is not formatted as a float.");
      } catch (System::ArgumentNullException*) {
         System::Console::WriteLine(S"The String* is 0.");
      }

      // Float to String* conversion will not overflow.
      stringVal = System::Convert::ToString(floatVal);
      System::Console::WriteLine(S"The float as a String* is {0}.", stringVal);
   }

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Convert クラス | Convert メンバ | System 名前空間 | Convert.ToSingle オーバーロードの一覧