Edit

Share via


Math.Truncate Method

Definition

Calculates the integral part of a number.

Overloads

Truncate(Decimal)

Calculates the integral part of a specified decimal number.

Truncate(Double)

Calculates the integral part of a specified double-precision floating-point number.

Remarks

The number is rounded to the nearest integer towards zero.

Truncate(Decimal)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Calculates the integral part of a specified decimal number.

C#
public static decimal Truncate(decimal d);

Parameters

d
Decimal

A number to truncate.

Returns

The integral part of d; that is, the number that remains after any fractional digits have been discarded.

Examples

The following example calls the Truncate(Decimal) method to truncate both a positive and a negative Decimal value.

C#
decimal decimalNumber;

decimalNumber = 32.7865m;
// Displays 32
Console.WriteLine(Math.Truncate(decimalNumber));

decimalNumber = -32.9012m;
// Displays -32
Console.WriteLine(Math.Truncate(decimalNumber));

Remarks

Truncate rounds d to the nearest integer towards zero.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Truncate(Double)

Source:
Math.cs
Source:
Math.cs
Source:
Math.cs

Calculates the integral part of a specified double-precision floating-point number.

C#
public static double Truncate(double d);

Parameters

d
Double

A number to truncate.

Returns

The integral part of d; that is, the number that remains after any fractional digits have been discarded, or one of the values listed in the following table.

Examples

The following example calls the Truncate(Double) method to truncate both a positive and a negative Double value.

C#
double floatNumber;

floatNumber = 32.7865;
// Displays 32
Console.WriteLine(Math.Truncate(floatNumber));

floatNumber = -32.9012;
// Displays -32
Console.WriteLine(Math.Truncate(floatNumber));

Remarks

Truncate rounds d to the nearest integer towards zero.

Starting with Visual Basic 15.8, the performance of Double-to-integer conversion is optimized if you pass the value returned by the Truncate method to the any of the integral conversion functions, or if the Double value returned by Truncate is automatically converted to an integer with Option Strict set to Off. This optimization allows code to run faster -- up to twice as fast for code that does a large number of conversions to integer types. The following example illustrates such an optimized conversion:

VB
Dim d As Double = 164.7194
Dim i As Integer = CInt(Math.Truncate(d))     ' Result: 164

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0