Export (0) Print
Expand All

Decimal.Truncate Method

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

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public static decimal Truncate (
	decimal d
)
public static Decimal Truncate (
	Decimal d
)
public static function Truncate (
	d : decimal
) : decimal
Not applicable.

Parameters

d

A Decimal to truncate.

Return Value

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

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

The following code example uses the Truncate method to discard the fractional digits of several Decimal values.

// 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
*/ 

// Example of the decimal.Negate, decimal.Floor, and decimal.Truncate 
// methods. 
import System.*;

class DecimalFloorNegTruncDemo
{
    private static String dataFmt = "{0,-30}{1,26}";

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

    public static void main(String[] args)
    {
        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(System.Convert.ToDecimal(0));
        ShowDecimalFloorNegTrunc(System.Convert.ToDecimal(123.456));
        ShowDecimalFloorNegTrunc(System.Convert.ToDecimal(-123.456));
        ShowDecimalFloorNegTrunc(new System.Decimal(1230000000, 0, 0, true,
            System.Convert.ToByte(7)));
        ShowDecimalFloorNegTrunc(System.Convert.ToDecimal(
            -9999999999.9999999999));
    } //main
} //DecimalFloorNegTruncDemo

/*
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
*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft