Edit

Bagikan melalui


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.

public:
 static System::Decimal Truncate(System::Decimal d);
public static decimal Truncate (decimal d);
static member Truncate : decimal -> decimal
Public Shared Function Truncate (d As Decimal) As Decimal

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.

decimal decimalNumber;

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

decimalNumber = -32.9012m;
// Displays -32
Console.WriteLine(Math.Truncate(decimalNumber));
   let decimalNumber = 32.7865m
   // Displays 32
   printfn $"{Math.Truncate decimalNumber}"

   let decimalNumber = -32.9012m
   // Displays -32
   printfn $"{Math.Truncate decimalNumber}"
Dim decimalNumber As Decimal

decimalNumber = 32.7865d
' Displays 32      
Console.WriteLine(Math.Truncate(decimalNumber))

decimalNumber = -32.9012d
' Displays -32
Console.WriteLine(Math.Truncate(decimalNumber))

Remarks

Truncate rounds d to the nearest integer towards zero.

See also

Applies to

Truncate(Double)

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

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

public:
 static double Truncate(double d);
public static double Truncate (double d);
static member Truncate : double -> double
Public Shared Function Truncate (d As Double) As Double

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.

d Return value
NaNNaN
NegativeInfinityNegativeInfinity
PositiveInfinityPositiveInfinity

Examples

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

double floatNumber;

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

floatNumber = -32.9012;
// Displays -32
Console.WriteLine(Math.Truncate(floatNumber));
   let floatNumber = 32.7865
   // Displays 32
   printfn $"{Math.Truncate floatNumber}"
   // printfn $"{truncate floatNumber}"

   let floatNumber = -32.9012
   // Displays -32
   printfn $"{Math.Truncate floatNumber}"
Dim floatNumber As Double

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:

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

See also

Applies to