C++ Standard Library C++ STL Library
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

C# - Math Truncate() Function



The C# Math Truncate() method is used to round the given number towards zero. It returns the nearest integral value with absolute value less than the argument. In special cases it returns the following:

  • If the argument is NaN, the method returns NaN.
  • If the argument is PositiveInfinity or NegativeInfinity, the method returns same as the argument.

Syntax

public static decimal Truncate (decimal x);

Parameters

x Specify a number.

Return Value

Returns the nearest integral value with absolute value less than the argument.

Example:

In the example below, Truncate() function returns the nearest integral value with absolute value less than the argument.

using System;

class MyProgram {
  static void Main(string[] args) {
   Console.WriteLine(Math.Truncate(2.5));
   Console.WriteLine(Math.Truncate(5.78));
   Console.WriteLine(Math.Truncate(-3.5));
   Console.WriteLine(Math.Truncate(-10.33));
  }
}

The output of the above code will be:

2
5
-3
-10

❮ C# Math Methods

5