指定した Object の値を単精度浮動小数点数に変換します。
Overloads Public Shared Function ToSingle( _
ByVal value As Object _) As Single
[C#]
public static float ToSingle(objectvalue);
[C++]
public: static float ToSingle(Object* value);
[JScript]
public static function ToSingle(
value : Object) : float;
パラメータ
- value
IConvertible インターフェイスを実装する Object か、または null 参照 (Visual Basic では Nothing) 。
戻り値
value の値と等価な単精度浮動小数点数。value が null 参照 (Visual Basic では Nothing) の場合は 0。
例外
例外の種類 | 条件 |
---|---|
InvalidCastException | value は IConvertible を実装しません。 |
解説
戻り値は、 value の基になる型の IConvertible.ToSingle メソッドを呼び出した結果得られる値です。
使用例
[Visual Basic, C#, C++] ToSingle を使用して、 String を Single に変換する方法については、次のコード例を参照してください。
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
参照
Convert クラス | Convert メンバ | System 名前空間 | Convert.ToSingle オーバーロードの一覧