Export (0) Print
Expand All

Decimal.Truncate Method

Returns the integral digits of the specified Decimal; any fractional digits are discarded.

[Visual Basic]
Public Shared Function Truncate( _
   ByVal d As Decimal _
) As Decimal
[C#]
public static decimal Truncate(
 decimal d
);
[C++]
public: static Decimal Truncate(
 Decimal d
);
[JScript]
public static function Truncate(
   d : Decimal
) : Decimal;

Parameters

d
A Decimal to truncate.

Return Value

The Decimal result of d rounded toward zero, to the nearest whole number.

Remarks

This method rounds d toward zero, to the nearest whole number, which corresponds to discarding any digits after the decimal point.

Example

[Visual Basic, C#, C++] The following code example uses the Truncate method to discard the fractional digits of several Decimal values.

[Visual Basic] 
' Example of the Decimal.Negate, Decimal.Floor, and Decimal.Truncate 
' methods. 
Imports System
Imports Microsoft.VisualBasic

Module DecimalFloorNegTruncDemo
    
    Const dataFmt As String = "{0,-30}{1,26}"

    ' Display Decimal parameters and their product, quotient, and 
    ' remainder.
    Sub ShowDecimalFloorNegTrunc( Argument as Decimal )

        Console.WriteLine( )
        Console.WriteLine( dataFmt, "Decimal Argument", Argument )
        Console.WriteLine( dataFmt, _
            "Decimal.Negate( Argument )", _
            Decimal.Negate( Argument ) )
        Console.WriteLine( dataFmt, _
            "Decimal.Floor( Argument )", _
            Decimal.Floor( Argument ) )
        Console.WriteLine( dataFmt, _
            "Decimal.Truncate( Argument )", _
            Decimal.Truncate( Argument ) )
    End Sub

    Sub Main( )
        Console.WriteLine( "This example of the " & vbCrLf & _
            "  Decimal.Negate( Decimal ), " & vbCrLf & _
            "  Decimal.Floor( Decimal ), and " & vbCrLf & _
            "  Decimal.Truncate( Decimal ) " & vbCrLf & _
            "methods generates the following output." )

        ' Create pairs of Decimal objects.
        ShowDecimalFloorNegTrunc( 0D ) 
        ShowDecimalFloorNegTrunc( 123.456D ) 
        ShowDecimalFloorNegTrunc( -123.456D ) 
        ShowDecimalFloorNegTrunc( _
            new Decimal( 1230000000, 0, 0, True, 7 ) )
        ShowDecimalFloorNegTrunc( -9999999999.9999999999D )
    End Sub
End Module 

' This example of the
'   Decimal.Negate( Decimal ),
'   Decimal.Floor( Decimal ), and
'   Decimal.Truncate( Decimal )
' methods generates the following output.
' 
' Decimal Argument                                       0
' Decimal.Negate( Argument )                             0
' Decimal.Floor( Argument )                              0
' Decimal.Truncate( Argument )                           0
' 
' Decimal Argument                                 123.456
' Decimal.Negate( Argument )                      -123.456
' Decimal.Floor( Argument )                            123
' Decimal.Truncate( Argument )                         123
' 
' Decimal Argument                                -123.456
' Decimal.Negate( Argument )                       123.456
' Decimal.Floor( Argument )                           -124
' Decimal.Truncate( Argument )                        -123
' 
' Decimal Argument                            -123.0000000
' Decimal.Negate( Argument )                   123.0000000
' Decimal.Floor( Argument )                           -123
' Decimal.Truncate( Argument )                        -123
' 
' Decimal Argument                  -9999999999.9999999999
' Decimal.Negate( Argument )         9999999999.9999999999
' Decimal.Floor( Argument )                   -10000000000
' Decimal.Truncate( Argument )                 -9999999999

[C#] 
// Example of the decimal.Negate, decimal.Floor, and decimal.Truncate 
// methods. 
using System;

class DecimalFloorNegTruncDemo
{
    const string dataFmt = "{0,-30}{1,26}";

    // Display decimal parameters and the method results.
    public static void ShowDecimalFloorNegTrunc( decimal Argument )
    {
        Console.WriteLine( );
        Console.WriteLine( dataFmt, "decimal Argument", Argument );
        Console.WriteLine( dataFmt, "decimal.Negate( Argument )", 
            decimal.Negate( Argument ) );
        Console.WriteLine( dataFmt, "decimal.Floor( Argument )", 
            decimal.Floor( Argument ) );
        Console.WriteLine( dataFmt, "decimal.Truncate( Argument )", 
            decimal.Truncate( Argument ) );
    }

    public static void Main( )
    {
        Console.WriteLine( "This example of the \n" +
            "  decimal.Negate( decimal ), \n" +
            "  decimal.Floor( decimal ), and \n" +
            "  decimal.Truncate( decimal ) \n" +
            "methods generates the following output." );

        // Create pairs of decimal objects.
        ShowDecimalFloorNegTrunc( 0M );
        ShowDecimalFloorNegTrunc( 123.456M );
        ShowDecimalFloorNegTrunc( -123.456M );
        ShowDecimalFloorNegTrunc( 
            new decimal( 1230000000, 0, 0, true, 7 ) );
        ShowDecimalFloorNegTrunc( -9999999999.9999999999M );
    }
}

/*
This example of the
  decimal.Negate( decimal ),
  decimal.Floor( decimal ), and
  decimal.Truncate( decimal )
methods generates the following output.

decimal Argument                                       0
decimal.Negate( Argument )                             0
decimal.Floor( Argument )                              0
decimal.Truncate( Argument )                           0

decimal Argument                                 123.456
decimal.Negate( Argument )                      -123.456
decimal.Floor( Argument )                            123
decimal.Truncate( Argument )                         123

decimal Argument                                -123.456
decimal.Negate( Argument )                       123.456
decimal.Floor( Argument )                           -124
decimal.Truncate( Argument )                        -123

decimal Argument                            -123.0000000
decimal.Negate( Argument )                   123.0000000
decimal.Floor( Argument )                           -123
decimal.Truncate( Argument )                        -123

decimal Argument                  -9999999999.9999999999
decimal.Negate( Argument )         9999999999.9999999999
decimal.Floor( Argument )                   -10000000000
decimal.Truncate( Argument )                 -9999999999
*/ 

[C++] 
// Example of the Decimal::Negate, Decimal::Floor, and 
// Decimal::Truncate methods. 
#using <mscorlib.dll>
using namespace System;

// Display Decimal parameters and the method results.
void ShowDecimalFloorNegTrunc( Decimal Argument )
{
    String* dataFmt = S"{0,-30}{1,26}";

    Console::WriteLine( );
    Console::WriteLine( dataFmt, S"Decimal Argument", 
        __box( Argument ) );
    Console::WriteLine( dataFmt, S"Decimal::Negate( Argument )", 
        __box( Decimal::Negate( Argument ) ) );
    Console::WriteLine( dataFmt, S"Decimal::Floor( Argument )", 
        __box( Decimal::Floor( Argument ) ) );
    Console::WriteLine( dataFmt, S"Decimal::Truncate( Argument )", 
        __box( Decimal::Truncate( Argument ) ) );
}

void main( )
{
    Console::WriteLine( S"This example of the \n" 
        S"  Decimal::Negate( Decimal ), \n" 
        S"  Decimal::Floor( Decimal ), and \n" 
        S"  Decimal::Truncate( Decimal ) \n" 
        S"methods generates the following output." );

    // Create pairs of Decimal objects.
    ShowDecimalFloorNegTrunc( Decimal::Parse( S"0" ) );
    ShowDecimalFloorNegTrunc( Decimal::Parse( S"123.456" ) );
    ShowDecimalFloorNegTrunc( Decimal::Parse( S"-123.456" ) );
    ShowDecimalFloorNegTrunc( Decimal( 1230000000, 0, 0, true, 7 ) );
    ShowDecimalFloorNegTrunc( 
        Decimal::Parse( S"-9999999999.9999999999" ) );
}

/*
This example of the
  Decimal::Negate( Decimal ),
  Decimal::Floor( Decimal ), and
  Decimal::Truncate( Decimal )
methods generates the following output.

Decimal Argument                                       0
Decimal::Negate( Argument )                            0
Decimal::Floor( Argument )                             0
Decimal::Truncate( Argument )                          0

Decimal Argument                                 123.456
Decimal::Negate( Argument )                     -123.456
Decimal::Floor( Argument )                           123
Decimal::Truncate( Argument )                        123

Decimal Argument                                -123.456
Decimal::Negate( Argument )                      123.456
Decimal::Floor( Argument )                          -124
Decimal::Truncate( Argument )                       -123

Decimal Argument                            -123.0000000
Decimal::Negate( Argument )                  123.0000000
Decimal::Floor( Argument )                          -123
Decimal::Truncate( Argument )                       -123

Decimal Argument                  -9999999999.9999999999
Decimal::Negate( Argument )        9999999999.9999999999
Decimal::Floor( Argument )                  -10000000000
Decimal::Truncate( Argument )                -9999999999
*/ 

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Decimal Structure | Decimal Members | System Namespace | Round | Floor

Show:
© 2014 Microsoft