次の方法で共有


Convert.ToDecimal メソッド (String)

指定した String 形式の数値を、それと等価な Decimal の数値に変換します。

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

パラメータ

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

戻り値

value の値と等価な Decimal の数値。

または

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

例外

例外の種類 条件
FormatException value が、省略可能な符号、0 から 9 までの一連の数字、省略可能な小数点記号の順で構成されていません。
OverflowException value が MinValue 未満の数値か、 MaxValue より大きい数値を表しています。

解説

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

使用例

[Visual Basic, C#, C++] ToDecimal を使用した StringDecimal への変換と、変換中にスローされる可能性がある例外については、次のコード例を参照してください。

 
Public Sub ConvertStringDecimal(ByVal stringVal As String)
    Dim decimalVal As Decimal = 0

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

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

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

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

[C++] 

   void ConvertStringDecimal(String* stringVal)
   {
      Decimal decimalVal = 0;

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

      // Decimal to String* conversion will not overflow.
      stringVal = System::Convert::ToString(decimalVal);
      System::Console::WriteLine(S"The decimal 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.ToDecimal オーバーロードの一覧