次の方法で共有


Decimal.ToSingle メソッド

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

Public Shared Function ToSingle( _
   ByVal d As Decimal _) As Single
[C#]
public static float ToSingle(decimald);
[C++]
public: static float ToSingle(Decimald);
[JScript]
public static function ToSingle(
   d : Decimal) : float;

パラメータ

戻り値

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

解説

単精度浮動小数点数の有効桁数が Decimal より少ないため、この演算によって丸め誤差が発生することがあります。

使用例

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

 
' Example of the Decimal.ToSingle and Decimal.ToDouble methods.
Imports System
Imports Microsoft.VisualBasic

Module DecimalToSgl_DblDemo

    Dim formatter As String = "{0,30}{1,17}{2,23}"

    ' Convert the Decimal argument; no exceptions are thrown.
    Sub DecimalToSgl_Dbl( argument As Decimal )

        Dim SingleValue   As Object
        Dim DoubleValue   As Object

        ' Convert the argument to a Single value.
        SingleValue = Decimal.ToSingle( argument )

        ' Convert the argument to a Double value.
        DoubleValue = Decimal.ToDouble( argument )

        Console.WriteLine( formatter, argument, _
            SingleValue, DoubleValue )
    End Sub

    Sub Main( )

        Console.WriteLine( "This example of the " & vbCrLf & _
            "  Decimal.ToSingle( Decimal ) and " & vbCrLf & _
            "  Decimal.ToDouble( Decimal ) " & vbCrLf & "methods " & _
            "generates the following output. It " & vbCrLf & _
            "displays several converted Decimal values." & vbCrLf )
        Console.WriteLine( formatter, "Decimal argument", _
            "Single", "Double" )
        Console.WriteLine( formatter, "----------------", _
            "------", "------" )

        ' Convert Decimal values and display the results.
        DecimalToSgl_Dbl( 0.0000000000000000000000000001D )
        DecimalToSgl_Dbl( 0.0000000000112233445566778899D )
        DecimalToSgl_Dbl( 123D )
        DecimalToSgl_Dbl( New Decimal( 123000000, 0, 0, False, 6 ) )
        DecimalToSgl_Dbl( 123456789.123456789D )
        DecimalToSgl_Dbl( 123456789123456789123456789D )
        DecimalToSgl_Dbl( Decimal.MinValue )
        DecimalToSgl_Dbl( Decimal.MaxValue )
    End Sub 
End Module 

' This example of the
'   Decimal.ToSingle( Decimal ) and
'   Decimal.ToDouble( Decimal )
' methods generates the following output. It
' displays several converted Decimal values.
' 
'               Decimal argument           Single                 Double
'               ----------------           ------                 ------
' 0.0000000000000000000000000001            1E-28                  1E-28
' 0.0000000000112233445566778899     1.122334E-11   1.12233445566779E-11
'                            123              123                    123
'                     123.000000              123                    123
'            123456789.123456789     1.234568E+08       123456789.123457
'    123456789123456789123456789     1.234568E+26   1.23456789123457E+26
' -79228162514264337593543950335    -7.922816E+28  -7.92281625142643E+28
'  79228162514264337593543950335     7.922816E+28   7.92281625142643E+28

[C#] 
// Example of the decimal.ToSingle and decimal.ToDouble methods.
using System;

class DecimalToSgl_DblDemo
{
    static string formatter = "{0,30}{1,17}{2,23}";

    // Convert the decimal argument; no exceptions are thrown.
    public static void DecimalToSgl_Dbl( decimal argument )
    {
        object SingleValue;
        object DoubleValue;

        // Convert the argument to a float value.
        SingleValue = decimal.ToSingle( argument );

        // Convert the argument to a double value.
        DoubleValue = decimal.ToDouble( argument );

        Console.WriteLine( formatter, argument, 
            SingleValue, DoubleValue );
    }

    public static void Main( )
    {
        Console.WriteLine( "This example of the \n" +
            "  decimal.ToSingle( decimal ) and \n" +
            "  decimal.ToDouble( decimal ) \nmethods " +
            "generates the following output. It \ndisplays " +
            "several converted decimal values.\n" );
        Console.WriteLine( formatter, "decimal argument", 
            "float", "double" );
        Console.WriteLine( formatter, "----------------", 
            "-----", "------" );

        // Convert decimal values and display the results.
        DecimalToSgl_Dbl( 0.0000000000000000000000000001M );
        DecimalToSgl_Dbl( 0.0000000000123456789123456789M );
        DecimalToSgl_Dbl( 123M );
        DecimalToSgl_Dbl( new decimal( 123000000, 0, 0, false, 6 ) );
        DecimalToSgl_Dbl( 123456789.123456789M );
        DecimalToSgl_Dbl( 123456789123456789123456789M );
        DecimalToSgl_Dbl( decimal.MinValue );
        DecimalToSgl_Dbl( decimal.MaxValue );
    }
}

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

              decimal argument            float                 double
              ----------------            -----                 ------
0.0000000000000000000000000001            1E-28                  1E-28
0.0000000000123456789123456789     1.234568E-11   1.23456789123457E-11
                           123              123                    123
                    123.000000              123                    123
           123456789.123456789     1.234568E+08       123456789.123457
   123456789123456789123456789     1.234568E+26   1.23456789123457E+26
-79228162514264337593543950335    -7.922816E+28  -7.92281625142643E+28
 79228162514264337593543950335     7.922816E+28   7.92281625142643E+28
*/

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

static __wchar_t* formatter = L"{0,30}{1,17}{2,23}";

// Convert the Decimal argument; no exceptions are thrown.
void DecimalToSgl_Dbl( Decimal argument )
{
    Object* SingleValue;
    Object* DoubleValue;

    // Convert the argument to a float value.
    SingleValue = __box( Decimal::ToSingle( argument ) );

    // Convert the argument to a double value.
    DoubleValue = __box( Decimal::ToDouble( argument ) );

    Console::WriteLine( formatter, __box( argument ), 
        SingleValue, DoubleValue );
}

void main( )
{
    Console::WriteLine( S"This example of the \n" 
        S"  Decimal::ToSingle( Decimal ) and \n" 
        S"  Decimal::ToDouble( Decimal ) \nmethods " 
        S"generates the following output. It \ndisplays " 
        S"several converted Decimal values.\n" );
    Console::WriteLine( formatter, S"Decimal argument", 
        S"float", S"double" );
    Console::WriteLine( formatter, S"----------------", 
        S"-----", S"------" );

    // Convert Decimal values and display the results.
    DecimalToSgl_Dbl( Decimal::Parse( "0.0000000000000000000000000001" ) );
    DecimalToSgl_Dbl( Decimal::Parse( "0.0000000000123456789123456789" ) );
    DecimalToSgl_Dbl( Decimal::Parse( "123" ) );
    DecimalToSgl_Dbl( Decimal( 123000000, 0, 0, false, 6 ) );
    DecimalToSgl_Dbl( Decimal::Parse( "123456789.123456789" ) );
    DecimalToSgl_Dbl( Decimal::Parse( "123456789123456789123456789" ) );
    DecimalToSgl_Dbl( Decimal::MinValue );
    DecimalToSgl_Dbl( Decimal::MaxValue );
}

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

              Decimal argument            float                 double
              ----------------            -----                 ------
0.0000000000000000000000000001            1E-28                  1E-28
0.0000000000123456789123456789     1.234568E-11   1.23456789123457E-11
                           123              123                    123
                    123.000000              123                    123
           123456789.123456789     1.234568E+08       123456789.123457
   123456789123456789123456789     1.234568E+26   1.23456789123457E+26
-79228162514264337593543950335    -7.922816E+28  -7.92281625142643E+28
 79228162514264337593543950335     7.922816E+28   7.92281625142643E+28
*/

[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 名前空間 | Single