You currently have JavaScript disabled on your web browser.

This website uses JavaScript, and This web page needs JavaScript activated to work correctly.

Please active JavaScript on your web browser and then refresh this web page.

C# provides a `System.Math`

class, that offers many powerful and useful fields and methods, which allow you to perform various mathematical operations and tasks on numbers.

Math is a very important aspect of programming, and almost all programs use Math to some degree. This `System.Math`

class makes it much easier for your program to perform more advanced mathematical tasks by providing you with well written canned functions (so that you do not have to write your own math functions).

`System.Math`

class provides the following ready to use fields:

Method | Description |
---|---|

E | Represents the natural logarithmic base, specified by the constant, e. |

PI | Represents the ratio of the circumference of a circle to its diameter, specified by the constant, π. |

Tau | Represents the number of radians in one turn, specified by the constant, τ. |

`System.Math`

class provides the following ready to use methods. Depending on the method, the supplied number value for **n** might be a decimal, double-precision floating-point, or integer number:

Method | Description |
---|---|

Abs(n) | Returns the absolute value of n . |

Acos(n) | Returns the angle whose cosine is the specified number. |

Acosh(n) | Returns the angle whose hyperbolic cosine is the specified number. |

Asin(n) | Returns the angle whose sine is the specified number. |

Asinh(n) | Returns the angle whose hyperbolic sine is the specified number. |

Atan(n) | Returns the angle whose tangent is the specified number. |

Atan2(n1, n2) | Returns the angle whose tangent is the quotient of two specified numbers. |

Atanh(n) | Returns the angle whose hyperbolic tangent is the specified number. |

BigMul(n1, n2) | Produces the full product of two numbers. |

BitDecrement(n) | Returns the largest value that compares less than a specified value. |

BitIncrement(n) | Returns the smallest value that compares greater than a specified value. |

Cbrt(n) | Returns the cube root of a specified number. |

Ceiling(n) | Returns the smallest integral value that is greater than or equal to the specified decimal number. |

Ceiling(n) | Returns the smallest integral value that is greater than or equal to the specified double-precision floating-point number. |

Clamp(n1, n2, n3) | Returns value clamped to the inclusive range of min and max. |

CopySign(n1, n2) | Returns a value with the magnitude of x and the sign of y. |

Cos(n) | Returns the cosine of the specified angle. |

Cosh(n) | Returns the hyperbolic cosine of the specified angle. |

DivRem(n1, n2) | Produces the quotient and the remainder of two numbers. |

DivRem(n1, n2, n3) | Calculates the quotient of two signed numbers and also returns the remainder in an output parameter. |

Exp(n) | Returns e raised to the specified power. |

Floor(n) | Returns the largest integral value less than or equal to the specified number. |

FusedMultiplyAdd(n1, n2, n3) | Returns (x * y) + z, rounded as one ternary operation. |

IEEERemainder(n1, n2) | Returns the remainder resulting from the division of a specified number by another specified number. |

ILogB(n) | Returns the base 2 integer logarithm of a specified number. |

Log(n) | Returns the natural (base e) logarithm of a specified number. |

Log(n1, n2) | Returns the logarithm of a specified number in a specified base. |

Log10(n) | Returns the base 10 logarithm of a specified number. |

Log2(n) | Returns the base 2 logarithm of a specified number. |

Max(n1, n2) | Returns the larger of two numbers. |

MaxMagnitude(n1, n2) | Returns the larger magnitude of two numbers. |

Min(n1, n2) | Returns the smaller of two numbers. |

MinMagnitude(n1, n2) | Returns the smaller magnitude of two numbers. |

Pow(n1, n2) | Returns a specified number raised to the specified power. |

ReciprocalEstimate(n) | Returns an estimate of the reciprocal of a specified number. |

ReciprocalSqrtEstimate(n) | Returns an estimate of the reciprocal square root of a specified number. |

Round(n) | Rounds a value to the nearest integral value, and rounds midpoint values to the nearest even number. |

Round(n1, n2) | Rounds a value to a specified number of fractional digits, and rounds midpoint values to the nearest even number. |

Round(n1, n2, MidpointRounding) | Rounds a value to a specified number of fractional digits using the specified rounding convention. |

ScaleB(n1, n2) | Returns x * 2^n computed efficiently. |

Sign(n) | Returns an integer that indicates the sign of a number. |

Sin(n) | Returns the sine of the specified angle. |

SinCos(n) | Returns the sine and cosine of the specified angle. |

Sinh(n) | Returns the hyperbolic sine of the specified angle. |

Sqrt(n) | Returns the square root of a specified number. |

Tan(n) | Returns the tangent of the specified angle. |

Tanh(n) | Returns the hyperbolic tangent of the specified angle. |

Truncate(n) | Calculates the integral part of a specified number. |

Truncate(n) | Calculates the integral part of a specified number. |

The following is a code example showing how to use the `System.Math`

class to round a decimal number to the nearest integer value:

using System ;
namespace MyApp
{
class MainProgram
{
static void Main (string [] args )
{
Console .WriteLine (Math .Round(9.968352 ));
}
}
}

The output is: 10

Since the `Math.Round()`

method has an overload for allowing you to specify the decimal number to round a decimal number to, the following is a code example showing how to use the `System.Math`

class to round a decimal number to two decimal places:

using System ;
namespace MyApp
{
class MainProgram
{
static void Main (string [] args )
{
Console .WriteLine (Math .Round(9.968352 , 2 ));
}
}
}

The output is: 9.97

Thank you for reading, I hope you found this blog post (tutorial) educational and helpful.

About | Contact Us | Privacy | Terms & Conditions | © 2024 - T&J Divisions, LLC, All Rights Reserved |