次の方法で共有


Decimal.ToUInt16 メソッド

指定した Decimal の値を、等価の 16 ビット符号なし整数に変換します。

このメソッドは、CLS と互換性がありません。CLS との互換性が必要な場合は、代わりに ToInt32 メンバを使用してください。CLS との互換性に関する詳細については 「共通言語仕様の概要」 を参照してください。

<CLSCompliant(False)>
Public Shared Function ToUInt16( _   ByVal value As Decimal _) As UInt16
[C#]
[CLSCompliant(false)]
public static ushort ToUInt16(decimalvalue);
[C++]
[CLSCompliant(false)]
public: static unsigned short ToUInt16(Decimalvalue);
[JScript]
public
   CLSCompliant(false)
static function ToUInt16(value : Decimal) : UInt16;

パラメータ

  • value
    変換する Decimal 値。

戻り値

value の値と等価な 16 ビット符号なし整数。

例外

例外の種類 条件
OverflowException value が UInt16.MaxValue より大きい値か、 UInt16.MinValue より小さい値です。

解説

戻り値は 10 進値の整数部分です。小数の桁は切り捨てられます。

使用例

[Visual Basic, C#, C++] ToUInt16 メソッドを使用して、 Decimal の数値を UInt16 の値に変換するコード例を次に示します。

 
' Example of the Decimal.ToInt16 and Decimal.ToUInt16 methods.
Imports System
Imports Microsoft.VisualBasic

Module DecimalToU_Int16Demo

    Dim formatter As String = "{0,16}{1,19}{2,19}"

    ' Get the exception type name; remove the namespace prefix.
    Function GetExceptionType( ex As Exception ) As String

        Dim exceptionType   As String = ex.GetType( ).ToString( )
        Return  exceptionType.Substring( _
            exceptionType.LastIndexOf( "."c ) + 1 )
    End Function

    ' Convert the Decimal argument; catch exceptions that are thrown.
    Sub DecimalToU_Int16( argument As Decimal )

        Dim Int16Value    As Object
        Dim UInt16Value   As Object

        ' Convert the argument to a Short value.
        Try
            Int16Value = Decimal.ToInt16( argument )
        Catch ex As Exception
            Int16Value = GetExceptionType( ex )
        End Try

        ' Convert the argument to a UInt16 value.
        Try
            UInt16Value = Decimal.ToUInt16( argument )
        Catch ex As Exception
            UInt16Value = GetExceptionType( ex )
        End Try

        Console.WriteLine( formatter, argument, _
            Int16Value, UInt16Value )
    End Sub

    Sub Main( )

        Console.WriteLine( "This example of the " & vbCrLf & _
            "  Decimal.ToInt16( Decimal ) and " & vbCrLf & _
            "  Decimal.ToUInt16( Decimal ) " & vbCrLf & "methods " & _
            "generates the following output. It " & vbCrLf & _
            "displays several converted Decimal values." & vbCrLf )
        Console.WriteLine( formatter, "Decimal argument", _
            "Short/exception", "UInt16/exception" )
        Console.WriteLine( formatter, "----------------", _
            "---------------", "----------------" )

        ' Convert Decimal values and display the results.
        DecimalToU_Int16( 123D )
        DecimalToU_Int16( New Decimal( 123000, 0, 0, False, 3 ) )
        DecimalToU_Int16( 123.999D )
        DecimalToU_Int16( 65535.999D )
        DecimalToU_Int16( 65536D )
        DecimalToU_Int16( 32767.999D )
        DecimalToU_Int16( 32768D )
        DecimalToU_Int16( - 0.999D )
        DecimalToU_Int16( - 1D )
        DecimalToU_Int16( - 32768.999D )
        DecimalToU_Int16( - 32769D )
    End Sub 
End Module 

' This example of the
'   Decimal.ToInt16( Decimal ) and
'   Decimal.ToUInt16( Decimal )
' methods generates the following output. It
' displays several converted Decimal values.
' 
' Decimal argument    Short/exception   UInt16/exception
' ----------------    ---------------   ----------------
'              123                123                123
'          123.000                123                123
'          123.999                123                123
'        65535.999  OverflowException              65535
'            65536  OverflowException  OverflowException
'        32767.999              32767              32767
'            32768  OverflowException              32768
'           -0.999                  0                  0
'               -1                 -1  OverflowException
'       -32768.999             -32768  OverflowException
'           -32769  OverflowException  OverflowException

[C#] 
// Example of the decimal.ToInt16 and decimal.ToUInt16 methods.
using System;

class DecimalToU_Int16Demo
{
    const string formatter = "{0,16}{1,19}{2,19}";

    // Get the exception type name; remove the namespace prefix.
    public static string GetExceptionType( Exception ex )
    {
        string exceptionType = ex.GetType( ).ToString( );
        return exceptionType.Substring( 
            exceptionType.LastIndexOf( '.' ) + 1 );
    }

    // Convert the decimal argument; catch exceptions that are thrown.
    public static void DecimalToU_Int16( decimal argument )
    {
        object Int16Value;
        object UInt16Value;

        // Convert the argument to a short value.
        try
        {
            Int16Value = decimal.ToInt16( argument );
        }
        catch( Exception ex )
        {
            Int16Value = GetExceptionType( ex );
        }

        // Convert the argument to a ushort value.
        try
        {
            UInt16Value = decimal.ToUInt16( argument );
        }
        catch( Exception ex )
        {
            UInt16Value = GetExceptionType( ex );
        }

        Console.WriteLine( formatter, argument, 
            Int16Value, UInt16Value );
    }

    public static void Main( )
    {
        Console.WriteLine( "This example of the \n" +
            "  decimal.ToInt16( decimal ) and \n" +
            "  decimal.ToUInt16( decimal ) \nmethods " +
            "generates the following output. It \ndisplays " +
            "several converted decimal values.\n" );
        Console.WriteLine( formatter, "decimal argument", 
            "short/exception", "ushort/exception" );
        Console.WriteLine( formatter, "----------------", 
            "---------------", "----------------" );

        // Convert decimal values and display the results.
        DecimalToU_Int16( 123M );
        DecimalToU_Int16( new decimal( 123000, 0, 0, false, 3 ) );
        DecimalToU_Int16( 123.999M );
        DecimalToU_Int16( 65535.999M );
        DecimalToU_Int16( 65536M );
        DecimalToU_Int16( 32767.999M );
        DecimalToU_Int16( 32768M );
        DecimalToU_Int16( - 0.999M );
        DecimalToU_Int16( - 1M );
        DecimalToU_Int16( - 32768.999M );
        DecimalToU_Int16( - 32769M );
    }
}

/*
This example of the
  decimal.ToInt16( decimal ) and
  decimal.ToUInt16( decimal )
methods generates the following output. It
displays several converted decimal values.

decimal argument    short/exception   ushort/exception
----------------    ---------------   ----------------
             123                123                123
         123.000                123                123
         123.999                123                123
       65535.999  OverflowException              65535
           65536  OverflowException  OverflowException
       32767.999              32767              32767
           32768  OverflowException              32768
          -0.999                  0                  0
              -1                 -1  OverflowException
      -32768.999             -32768  OverflowException
          -32769  OverflowException  OverflowException
*/

[C++] 
// Example of the Decimal::ToInt16 and Decimal::ToUInt16 methods.
#using <mscorlib.dll>
using namespace System;

const __wchar_t* formatter = L"{0,16}{1,19}{2,19}";

// Get the exception type name; remove the namespace prefix.
String* GetExceptionType( Exception* ex )
{
    String* exceptionType = ex->GetType( )->ToString( );
    return exceptionType->Substring( 
        exceptionType->LastIndexOf( '.' ) + 1 );
}

// Convert the Decimal argument; catch exceptions that are thrown.
void DecimalToU_Int16( Decimal argument )
{
    Object* Int16Value;
    Object* UInt16Value;

    // Convert the argument to a short value.
    try
    {
        Int16Value = __box( Decimal::ToInt16( argument ) );
    }
    catch( Exception* ex )
    {
        Int16Value = GetExceptionType( ex );
    }

    // Convert the argument to an unsigned short value.
    try
    {
        UInt16Value = __box( Decimal::ToUInt16( argument ) );
    }
    catch( Exception* ex )
    {
        UInt16Value = GetExceptionType( ex );
    }

    Console::WriteLine( formatter, __box( argument ), 
        Int16Value, UInt16Value );
}

void main( )
{
    Console::WriteLine( S"This example of the \n" 
        S"  Decimal::ToInt16( Decimal ) and \n" 
        S"  Decimal::ToUInt16( Decimal ) \nmethods " 
        S"generates the following output. It \ndisplays " 
        S"several converted Decimal values.\n" );
    Console::WriteLine( formatter, S"Decimal argument", 
        S"short/exception", S"unsigned short" );
    Console::WriteLine( formatter, S"----------------", 
        S"---------------", S"--------------" );

    // Convert Decimal values and display the results.
    DecimalToU_Int16( Decimal::Parse( "123" ) );
    DecimalToU_Int16( Decimal( 123000, 0, 0, false, 3 ) );
    DecimalToU_Int16( Decimal::Parse( "123.999" ) );
    DecimalToU_Int16( Decimal::Parse( "65535.999" ) );
    DecimalToU_Int16( Decimal::Parse( "65536" ) );
    DecimalToU_Int16( Decimal::Parse( "32767.999" ) );
    DecimalToU_Int16( Decimal::Parse( "32768" ) );
    DecimalToU_Int16( Decimal::Parse( "-0.999" ) );
    DecimalToU_Int16( Decimal::Parse( "-1" ) );
    DecimalToU_Int16( Decimal::Parse( "-32768.999" ) );
    DecimalToU_Int16( Decimal::Parse( "-32769" ) );
}

/*
This example of the
  Decimal::ToInt16( Decimal ) and
  Decimal::ToUInt16( Decimal )
methods generates the following output. It
displays several converted Decimal values.

Decimal argument    short/exception     unsigned short
----------------    ---------------     --------------
             123                123                123
         123.000                123                123
         123.999                123                123
       65535.999  OverflowException              65535
           65536  OverflowException  OverflowException
       32767.999              32767              32767
           32768  OverflowException              32768
          -0.999                  0                  0
              -1                 -1  OverflowException
      -32768.999             -32768  OverflowException
          -32769  OverflowException  OverflowException
*/

[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

参照

Decimal 構造体 | Decimal メンバ | System 名前空間 | UInt16