Truncate Method

Decimal.Truncate Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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
)

Parameters

d
Type: System.Decimal
A Decimal to truncate.

Return Value

Type: System.Decimal
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 Example
{
   const string dataFmt = "{0,-30}{1,26}";

   // Display decimal parameters and the method results.
   public static void ShowDecimalFloorNegTrunc(System.Windows.Controls.TextBlock outputBlock, decimal Argument)
   {
      outputBlock.Text += "\n";
      outputBlock.Text += String.Format(dataFmt, "decimal Argument", Argument) + "\n";
      outputBlock.Text += String.Format(dataFmt, "decimal.Negate( Argument )",
          decimal.Negate(Argument)) + "\n";
      outputBlock.Text += String.Format(dataFmt, "decimal.Floor( Argument )",
          decimal.Floor(Argument)) + "\n";
      outputBlock.Text += String.Format(dataFmt, "decimal.Truncate( Argument )",
          decimal.Truncate(Argument)) + "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += "This example of the \n" +
          "  decimal.Negate( decimal ), \n" +
          "  decimal.Floor( decimal ), and \n" +
          "  decimal.Truncate( decimal ) \n" +
          "methods generates the following output." + "\n";

      // Create pairs of decimal objects.
      ShowDecimalFloorNegTrunc(outputBlock, 0M);
      ShowDecimalFloorNegTrunc(outputBlock, 123.456M);
      ShowDecimalFloorNegTrunc(outputBlock, -123.456M);
      ShowDecimalFloorNegTrunc(outputBlock, 
          new decimal(1230000000, 0, 0, true, 7));
      ShowDecimalFloorNegTrunc(outputBlock, -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
*/


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft