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:
This method is used to compute an integral part of a specified decimal number.
public static decimal Truncate(decimal dec)
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.
This method is used to compute an integral part of a specified double precision floating point number.
public static double Truncate(decimal dob)
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.
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.
- Difference between Method Overriding and Method Hiding in C#
- C# | PadLeft() Method
- C# | Math.Abs() Method | Set - 2
- Extension Method in C#
- Stack.Contains() Method in C#
- C# | Math.Pow() Method
- C# | Math.Abs() Method | Set - 1
- Stack.Pop() Method in C#
- C# | Join() Method | Set - 1
- C# | Method Parameters
- C# | Trim() Method
- C# | ToCharArray() Method
- Method Hiding in C#
- C# | Math.Exp() Method
- C# | IndexOfAny() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.