次の方法で共有


Decimal から Char への変換

Decimal を Unicode 文字に変換します。

returnValue = Decimal.op_Explicit(value)
[C#]
public static explicit operator char(decimalvalue);
[C++]
public: static __wchar_t op_Explicit();
[JScript]
returnValue = Char(value);

[Visual Basic] Visual Basic では、この型で定義されている型変換を使用することができます。ただし、独自に定義することはできません。Decimal から Char への変換の代わりに、ToInt16 メソッドを使用できます。

[JScript] JScript では、この型で定義されている型変換を使用することができます。ただし、独自に定義することはできません。

引数 [Visual Basic, JScript]

パラメータ [C#]

戻り値

変換された Decimal を表す Unicode 文字。

例外

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

使用例

[C#, C++] 明示的な Decimal to Char 変換を使用して Decimal の数値を Char の値 (Unicode 文字) に変換するコード例を次に示します。

 
// Example of the explicit conversion from decimal to char.
using System;

class DecimalToCharDemo
{
    const string formatter = "{0,17}{1,19}{2,12}";

    // 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 DecimalToChar( decimal argument )
    {
        object CharValue;

        // Convert the argument to a char value.
        try
        {
            CharValue = (char)argument;
            Console.WriteLine( formatter, argument, CharValue, 
                ( (ushort)(char)CharValue ).ToString( "X4" ) );
        }
        catch( Exception ex )
        {
            CharValue = GetExceptionType( ex );
            Console.WriteLine( formatter, argument, CharValue, null );
        }
    }

    public static void Main( )
    {
        Console.WriteLine( 
            "This example of the explicit conversion from decimal " +
            "\nto char generates the following output. It " +
            "displays \nseveral converted decimal values.\n" );
        Console.WriteLine( formatter, "decimal argument", 
            "char/exception", "hex code" );
        Console.WriteLine( formatter, "----------------", 
            "--------------", "--------" );

        // Convert decimal values and display the results.
        DecimalToChar( 33.33M );
        DecimalToChar( 55.5M );
        DecimalToChar( 77.7M );
        DecimalToChar( 123M );
        DecimalToChar( 123.999M );
        DecimalToChar( 143.43M );
        DecimalToChar( 170M );
        DecimalToChar( 188.88M );
        DecimalToChar( 222M );
        DecimalToChar( 244M );
        DecimalToChar( 488M );
        DecimalToChar( 8217M );
        DecimalToChar( 8250M );
        DecimalToChar( 16383M );
        DecimalToChar( 32768M );
        DecimalToChar( 65535.999M );
        DecimalToChar( 65536M );
        DecimalToChar( - 0.999M );
        DecimalToChar( - 1M );
    }
}

/*
This example of the explicit conversion from decimal
to char generates the following output. It displays
several converted decimal values.

 decimal argument     char/exception    hex code
 ----------------     --------------    --------
            33.33                  !        0021
             55.5                  7        0037
             77.7                  M        004D
              123                  {        007B
          123.999                  {        007B
           143.43                  ?        008F
              170                          00AA
           188.88                          00BC
              222                  _        00DE
              244                          00F4
              488                  K        01E8
             8217                  '        2019
             8250                  >        203A
            16383                  ?        3FFF
            32768                  ?        8000
        65535.999                  ?        FFFF
            65536  OverflowException
           -0.999                           0000
               -1  OverflowException
*/

[C++] 
// Example of the explicit conversion from Decimal to __wchar_t.
#using <mscorlib.dll>
using namespace System;

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

// 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 DecimalToChar( Decimal argument )
{
    Object* CharValue;

    // Convert the argument to a __wchar_t value.
    try
    {
        CharValue = __box( (__wchar_t)argument );
        Console::WriteLine( formatter, __box( argument ), CharValue, 
            ( (unsigned short)
                (* static_cast<__box __wchar_t *>(CharValue) ) ).
                    ToString( S"X4" ) );
    }
    catch( Exception* ex )
    {
        CharValue = GetExceptionType( ex );
        Console::WriteLine( formatter, __box( argument ), 
            CharValue, String::Empty );
    }
}

void main( )
{
    Console::WriteLine( 
        S"This example of the explicit conversion from Decimal to " 
        S"\n__wchar_t generates the following output. It " 
        S"displays \nseveral converted Decimal values.\n" );
    Console::WriteLine( formatter, S"Decimal argument", 
        S"__wchar_t", S"hex code" );
    Console::WriteLine( formatter, S"----------------", 
        S"---------", S"--------" );

    // Convert Decimal values and display the results.
    DecimalToChar( Decimal::Parse( "33.33" ) );
    DecimalToChar( Decimal::Parse( "55.5" ) );
    DecimalToChar( Decimal::Parse( "77.7" ) );
    DecimalToChar( Decimal::Parse( "123" ) );
    DecimalToChar( Decimal::Parse( "123.999" ) );
    DecimalToChar( Decimal::Parse( "170" ) );
    DecimalToChar( Decimal::Parse( "188.88" ) );
    DecimalToChar( Decimal::Parse( "222" ) );
    DecimalToChar( Decimal::Parse( "244" ) );
    DecimalToChar( Decimal::Parse( "488" ) );
    DecimalToChar( Decimal::Parse( "8217" ) );
    DecimalToChar( Decimal::Parse( "8250" ) );
    DecimalToChar( Decimal::Parse( "16383" ) );
    DecimalToChar( Decimal::Parse( "65535.999" ) );
    DecimalToChar( Decimal::Parse( "65536" ) );
    DecimalToChar( Decimal::Parse( "-0.999" ) );
    DecimalToChar( Decimal::Parse( "-1" ) );
}

/*
This example of the explicit conversion from Decimal to
__wchar_t generates the following output. It displays
several converted Decimal values.

 Decimal argument          __wchar_t    hex code
 ----------------          ---------    --------
            33.33                  !        0021
             55.5                  7        0037
             77.7                  M        004D
              123                  {        007B
          123.999                  {        007B
              170                          00AA
           188.88                          00BC
              222                  _        00DE
              244                          00F4
              488                  K        01E8
             8217                  '        2019
             8250                  >        203A
            16383                  ?        3FFF
        65535.999                  ?        FFFF
            65536  OverflowException
           -0.999                           0000
               -1  OverflowException
*/

[Visual Basic, JScript] Visual Basic および JScript のサンプルはありません。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

参照

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