Decimal.Truncate Method
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Returns the integral digits of the specified Decimal; any fractional digits are discarded.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
<SecuritySafeCriticalAttribute> _
Public Shared Function Truncate ( _
d As Decimal _
) As Decimal
[SecuritySafeCriticalAttribute]
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.
Remarks
This method rounds d toward zero, to the nearest whole number, which corresponds to discarding any digits after the decimal point.
Examples
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.
Module Example
Const dataFmt As String = "{0,-30}{1,26}"
' Display Decimal parameters and their product, quotient, and
' remainder.
Sub ShowDecimalFloorNegTrunc(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal Argument As Decimal)
outputBlock.Text &= vbCrLf
outputBlock.Text &= String.Format(dataFmt, "Decimal Argument", Argument) & vbCrLf
outputBlock.Text &= String.Format(STring.Format(dataFmt, _
"Decimal.Negate( Argument )", _
Decimal.Negate(Argument))) & vbCrLf
outputBlock.Text &= String.Format(dataFmt, _
"Decimal.Floor( Argument )", _
Decimal.Floor(Argument)) & vbCrLf
outputBlock.Text &= String.Format(dataFmt, _
"Decimal.Truncate( Argument )", _
Decimal.Truncate(Argument)) & vbCrLf
End Sub
Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
outputBlock.Text &= "This example of the " & vbCrLf & _
" Decimal.Negate( Decimal ), " & vbCrLf & _
" Decimal.Floor( Decimal ), and " & vbCrLf & _
" Decimal.Truncate( Decimal ) " & vbCrLf & _
"methods generates the following output." & vbCrLf
' Create pairs of Decimal objects.
ShowDecimalFloorNegTrunc(outputBlock, 0D)
ShowDecimalFloorNegTrunc(outputBlock, 123.456D)
ShowDecimalFloorNegTrunc(outputBlock, -123.456D)
ShowDecimalFloorNegTrunc(outputBlock, _
New Decimal(1230000000, 0, 0, True, 7))
ShowDecimalFloorNegTrunc(outputBlock, -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
// 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
*/
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
XNA Framework
Supported in: Xbox 360, Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.