Open In App

C# | Math.Truncate() Method

Last Updated : 02 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In C#, Math.Truncate() is a math class method which is used to compute an integral part of a specified decimal number or double-precision floating-point number. This method can be overloaded by passing the different type of parameters to it as follows:

  • Math.Truncate(Decimal)
  • Math.Truncate(Double)

Math.Truncate(Decimal)

This method is used to compute an integral part of a specified decimal number.

Syntax: 

public static decimal Truncate(decimal dec)

Parameter:

dec: It is the specified number which is to be truncated and type of this parameter is System.Decimal.

Return Type: This method only return the integral part of dec and discard the fractional part. The type of this method is System.Decimal.

Example: 

C#




// C# Program to illustrate the
// Math.Truncate(Decimal) Method
using System;
  
class Geeks {
  
    // Main Method
    public static void Main()
    {
  
        // variables of Decimal type
        Decimal dec = 45.89511m;
        Decimal dec2 = 54569.478021m;
  
        // using function and displaying result
        Console.WriteLine(Math.Truncate(dec));
        Console.WriteLine(Math.Truncate(dec2));
    }
}


Output: 

45
54569

Math.Truncate(Double)

This method is used to compute an integral part of a specified double precision floating point number.

Syntax: 

public static double Truncate(decimal dob)

Parameter:

dob: It is the specified number which is to be truncated and type of this parameter is System.Double.

Return Type: This method only returns an integral part of dob and discard the fractional part. The type of this method is System.Double.

Note: If dob is NaN, then method will return NaN value and If dob is PositiveInfinity, then method will return PositiveInfinity value. If dob is NegativeInfinity, then method will return NegativeInfinity value.

Example: 

C#




// C# Program to illustrate the
// Math.Truncate(Double) Method
using System;
  
class Geeks {
  
    // Main Method
    public static void Main()
    {
  
        // variables of Double type
        Double dob = 45649.25649800;
        Double dob2 = 2000150.2654459780;
  
        // using function and displaying result
        Console.WriteLine(Math.Truncate(dob));
        Console.WriteLine(Math.Truncate(dob2));
    }
}


Output: 

45649
2000150

There can be other ways to truncate numbers like casting it to an int, but it does not always work. As compare to other Math methods, this is probably the most reliable way to perform the required tasks.
 



Similar Reads

MathF.Truncate() Method in C# with Examples
In C#, MathF.Truncate(Single) is a MathF class method which is used to compute an integral part of a specified single number or single-precision floating-point number.Syntax: public static float Truncate (float x); Parameter: x: It is the specified number which is to be truncated and type of this parameter is System.Single. Return Type: This method
1 min read
Decimal.Truncate() Method in C#
This method is used to get the integral digits of the specified Decimal by discarding any fractional digits. This method rounds the specified value to the nearest whole number by removing the digits after the decimal point. Syntax: public static decimal Truncate (decimal d); Here, d is the decimal number which is to be truncated. Return Value: It r
1 min read
C# | Math.IEEERemainder() Method
In C#, IEEERemainder() is a Math class method which is used to return the remainder resulting from the division of a specified number by another specified number. Syntax: public static double IEEERemainder (double a, double b); Parameters: a: It is the dividend of type System.Double.b: It is the divisor of type System.Double. Return Type: This meth
2 min read
C# | Math.Sign() Method
In C#, Sign() is a math class method which returns an integer that specify the sign of the number. This method can be overloaded by changing the data type of the passed arguments as follows: Math.Sign(Decimal): Returns the integer that specifies the sign of a decimal number. Math.Sign(Double): Returns the integer that specifies the sign of a double
3 min read
C# | Math.BigMul() Method
In C#, BigMul() is a method class method. This method is used to compute the full product of two 32-bit numbers.Syntax: public static long BigMul(int a, int b) Parameters: a: It is the first number to be multiplied and the type of this parameter is System.Int32. b: It is the Second number to be multiplied and the type of this parameter is System.In
1 min read
C# | Math.Pow() Method
In C#, Math.Pow() is a Math class method. This method is used to calculate a number raise to the power of some other number. Syntax: public static double Pow(double base, double power) Parameters: double base: It is a double-precision floating-point number which is to be raised to a power and type of this parameter is System.Double. double power: I
2 min read
C# | Math.Floor() Method
In C#, Math.Floor() is a Math class method. This method is used to find the largest integer, which is less than or equal to the passed argument. The floor method operates both functionalities in decimal and double. This method can be overload by passing different arguments to it. Math.Floor(Decimal) Method Math.Floor(Double) Method Math.Floor(Decim
3 min read
C# | Math.Tanh() Method
Math.Tanh() is the inbuilt Math class method which returns the hyperbolic tan of a given double value argument. The result will be NaN if the given argument is NaN.Syntax: public static double Tanh(double num) Parameter: num: It is the number whose hyperbolic tan is to be returned and type of this parameter is System.Double. Return Value: The metho
2 min read
C# | Math.Ceiling() Method
In C#, Math.Ceiling() is a Math class method. This method is used to find the smallest integer , which is greater than or equal to the passed argument. The Ceiling method operates both functionalities in decimal and double. This method can be overload by passing different arguments to it. Math.Ceiling(Decimal) MethodMath.Ceiling(Double) Method Math
3 min read
C# | Math.Sinh() Method
Math.Sinh() is the inbuilt Math class method which returns the hyperbolic sine of a given double value argument(specified angle).Syntax: public static double Sinh(double num) Parameters: num: It is the number whose hyperbolic sine is to be returned and type of this parameter is System.Double. Return Value: The method returns the hyperbolic Sine of
2 min read
Article Tags :