Table of Contents

Class MathHelper

Namespace
Ecng.Common
Assembly
Ecng.Common.dll

Provides various mathematical helper methods and extension methods for numeric types.

public static class MathHelper
Inheritance
MathHelper
Inherited Members

Methods

Abs(decimal)

Returns the absolute value of the decimal value.

public static decimal Abs(this decimal value)

Parameters

value decimal

The decimal value.

Returns

decimal

The absolute value.

Abs(double)

Returns the absolute value of the double value.

public static double Abs(this double value)

Parameters

value double

The double value.

Returns

double

The absolute value.

Abs(short)

Returns the absolute value of the short value.

public static short Abs(this short value)

Parameters

value short

The short integer.

Returns

short

The absolute value.

Abs(int)

Returns the absolute value of the integer value.

public static int Abs(this int value)

Parameters

value int

The integer value.

Returns

int

The absolute value.

Abs(long)

Returns the absolute value of the long integer value.

public static long Abs(this long value)

Parameters

value long

The long integer.

Returns

long

The absolute value.

Abs(sbyte)

Returns the absolute value of the sbyte value.

[CLSCompliant(false)]
public static sbyte Abs(this sbyte value)

Parameters

value sbyte

The sbyte value.

Returns

sbyte

The absolute value.

Abs(float)

Returns the absolute value of the float value.

public static float Abs(this float value)

Parameters

value float

The float value.

Returns

float

The absolute value.

Abs(TimeSpan)

Returns a TimeSpan whose ticks are the absolute value of the original TimeSpan's ticks.

public static TimeSpan Abs(this TimeSpan value)

Parameters

value TimeSpan

The TimeSpan value.

Returns

TimeSpan

The TimeSpan with absolute ticks.

Acos(decimal)

Returns the angle whose cosine is the specified decimal value.

public static decimal Acos(this decimal value)

Parameters

value decimal

A decimal value representing a cosine.

Returns

decimal

The angle, in radians, whose cosine is the specified value.

Acos(double)

Returns the angle whose cosine is the specified value.

public static double Acos(this double value)

Parameters

value double

A double value representing a cosine.

Returns

double

The angle, in radians, whose cosine is the specified value.

AsRaw(double)

Gets the raw 64-bit integer representation of the double value.

public static long AsRaw(this double value)

Parameters

value double

The double value.

Returns

long

The 64-bit integer representation.

AsRaw(int)

Converts an integer value to a single-precision float via a byte array conversion.

public static float AsRaw(this int value)

Parameters

value int

The integer value.

Returns

float

The float value.

AsRaw(long)

Converts a 64-bit integer representation to a double value.

public static double AsRaw(this long value)

Parameters

value long

The 64-bit integer representation.

Returns

double

The double value.

AsRaw(float)

Converts a float value to its raw 32-bit integer representation.

public static int AsRaw(this float value)

Parameters

value float

The float value.

Returns

int

The 32-bit integer representation.

Asin(decimal)

Returns the angle whose sine is the specified decimal value.

public static decimal Asin(this decimal value)

Parameters

value decimal

A decimal value representing a sine.

Returns

decimal

The angle, in radians, whose sine is the specified value.

Asin(decimal, decimal)

Returns the angle whose tangent is the quotient of two specified decimal numbers.

public static decimal Asin(this decimal x, decimal y)

Parameters

x decimal

The numerator.

y decimal

The denominator.

Returns

decimal

The angle, in radians, whose tangent is the quotient of x and y.

Asin(double)

Returns the angle whose sine is the specified value.

public static double Asin(this double value)

Parameters

value double

A double value representing a sine.

Returns

double

The angle, in radians, whose sine is the specified value.

Asin(double, double)

Returns the angle whose tangent is the quotient of two specified double numbers.

public static double Asin(this double x, double y)

Parameters

x double

The numerator.

y double

The denominator.

Returns

double

The angle, in radians, whose tangent is the quotient of x and y.

Atan(decimal)

Returns the angle whose tangent is the specified decimal value.

public static decimal Atan(this decimal value)

Parameters

value decimal

A decimal value representing a tangent.

Returns

decimal

The angle, in radians, whose tangent is the specified value.

Atan(double)

Returns the angle whose tangent is the specified value.

public static double Atan(this double value)

Parameters

value double

A double value representing a tangent.

Returns

double

The angle, in radians, whose tangent is the specified value.

BigMul(int, int)

Returns the full product of two integers.

public static long BigMul(this int x, int y)

Parameters

x int

The first integer.

y int

The second integer.

Returns

long

The product as a long.

Ceiling(decimal)

Returns the smallest integer greater than or equal to the specified value.

public static decimal Ceiling(this decimal value)

Parameters

value decimal

The decimal value.

Returns

decimal

The ceiled value.

Ceiling(decimal, decimal)

Returns the smallest multiple of step greater than or equal to the specified value.

public static decimal Ceiling(this decimal value, decimal step)

Parameters

value decimal

The decimal value.

step decimal

The step size.

Returns

decimal

The ceiled value.

Ceiling(double)

Returns the smallest integer greater than or equal to the specified double value as a long.

public static long Ceiling(this double value)

Parameters

value double

The double value.

Returns

long

The ceiled value as long.

Ceiling(float)

Returns the smallest integer greater than or equal to the float value.

public static int Ceiling(this float value)

Parameters

value float

The float value.

Returns

int

The ceiling integer.

Cos(decimal)

Returns the cosine of the specified decimal angle.

public static decimal Cos(this decimal value)

Parameters

value decimal

An angle, measured in radians (as decimal).

Returns

decimal

The cosine of the angle as a decimal.

Cos(double)

Returns the cosine of the specified double angle.

public static double Cos(this double value)

Parameters

value double

An angle, measured in radians.

Returns

double

The cosine of the angle.

Cosh(decimal)

Returns the hyperbolic cosine of the specified decimal angle.

public static decimal Cosh(this decimal value)

Parameters

value decimal

An angle, measured in radians (as decimal).

Returns

decimal

The hyperbolic cosine of the angle as a decimal.

Cosh(double)

Returns the hyperbolic cosine of the specified double angle.

public static double Cosh(this double value)

Parameters

value double

An angle, measured in radians.

Returns

double

The hyperbolic cosine of the angle.

DivRem(int, int, out int)

Computes the quotient and remainder of the division of two integers.

public static int DivRem(this int a, int b, out int result)

Parameters

a int

The dividend.

b int

The divisor.

result int

When the method returns, contains the remainder.

Returns

int

The quotient.

DivRem(long, long, out long)

Computes the quotient and remainder of the division of two long integers.

public static long DivRem(this long a, long b, out long result)

Parameters

a long

The dividend.

b long

The divisor.

result long

When the method returns, contains the remainder.

Returns

long

The quotient.

Exp(decimal)

Returns e raised to the specified decimal power.

public static decimal Exp(this decimal value)

Parameters

value decimal

A decimal exponent.

Returns

decimal

The value of e raised to the specified power as a decimal.

Exp(double)

Returns e raised to the specified double power.

public static double Exp(this double value)

Parameters

value double

A double exponent.

Returns

double

The value of e raised to the specified power.

ExtractMantissaExponent(decimal, out long, out int)

Extracts the mantissa and scale (exponent) from the decimal value.

public static void ExtractMantissaExponent(this decimal value, out long mantissa, out int exponent)

Parameters

value decimal

The decimal value.

mantissa long

The extracted mantissa.

exponent int

The extracted scale factor.

ExtractMantissaExponent(double, out long, out int)

Extracts the normalized mantissa and exponent from the double value.

public static void ExtractMantissaExponent(this double value, out long mantissa, out int exponent)

Parameters

value double

The double value.

mantissa long

The extracted mantissa.

exponent int

The extracted exponent.

Floor(decimal)

Returns the largest integer less than or equal to the specified value.

public static decimal Floor(this decimal value)

Parameters

value decimal

The decimal value.

Returns

decimal

The floored value.

Floor(decimal, decimal)

Returns the largest multiple of step less than or equal to the specified value.

public static decimal Floor(this decimal value, decimal step)

Parameters

value decimal

The decimal value.

step decimal

The step size.

Returns

decimal

The floored value.

Floor(double)

Returns the largest integer less than or equal to the specified double value as a long.

public static long Floor(this double value)

Parameters

value double

The double value.

Returns

long

The floored value as long.

Floor(double, double)

Returns the largest multiple of step less than or equal to the double value.

public static double Floor(this double value, double step)

Parameters

value double

The double value.

step double

The step size.

Returns

double

The floored value.

Floor(int, int)

Returns the largest multiple of step less than or equal to the integer value.

public static int Floor(this int value, int step)

Parameters

value int

The integer value.

step int

The step size.

Returns

int

The floored value.

Floor(long, long)

Returns the largest multiple of step less than or equal to the long integer value.

public static long Floor(this long value, long step)

Parameters

value long

The long integer value.

step long

The step size.

Returns

long

The floored value.

Floor(float)

Returns the largest integer less than or equal to the float value.

public static int Floor(this float value)

Parameters

value float

The float value.

Returns

int

The floor integer.

Floor(float, float)

Returns the largest multiple of step less than or equal to the float value.

public static float Floor(this float value, float step)

Parameters

value float

The float value.

step float

The step size.

Returns

float

The floored value.

GetBit(byte, int)

Determines whether the bit at the specified 1-based index in the byte is set.

public static bool GetBit(this byte value, int index)

Parameters

value byte

The byte value.

index int

The 1-based index of the bit to test.

Returns

bool

true if the specified bit is set; otherwise, false.

GetBit(int, int)

Gets the state of the bit at the specified index in an integer.

public static bool GetBit(this int value, int index)

Parameters

value int

The integer value.

index int

The bit index (0-based).

Returns

bool

True if the bit is set; otherwise, false.

GetBit(long, int)

Gets the state of the bit at the specified index in a long value.

public static bool GetBit(this long value, int index)

Parameters

value long

The long value.

index int

The bit index (0-based).

Returns

bool

True if the bit is set; otherwise, false.

GetCachedDecimals(decimal)

Gets the effective scale (number of significant decimal places) of the decimal value, using caching for performance.

public static int GetCachedDecimals(this decimal value)

Parameters

value decimal

The decimal value.

Returns

int

The effective scale of the decimal.

GetDecimalInfo(decimal)

Retrieves detailed information about the decimal value, including mantissa, precision, scale, and trailing zeros.

public static MathHelper.DecimalInfo GetDecimalInfo(this decimal value)

Parameters

value decimal

The decimal value.

Returns

MathHelper.DecimalInfo

A MathHelper.DecimalInfo structure containing detailed decimal information.

GetDigitCount(int)

Returns the number of digits in a positive integer.

public static int GetDigitCount(this int x)

Parameters

x int

The positive integer value.

Returns

int

The digit count.

Exceptions

ArgumentOutOfRangeException

Thrown when x is negative.

GetDigitCount(long)

Returns the number of digits in a positive long integer.

public static int GetDigitCount(this long x)

Parameters

x long

The positive long integer value.

Returns

int

The digit count.

Exceptions

ArgumentOutOfRangeException

Thrown when x is negative.

GetMiddle(decimal, decimal)

Calculates the middle (average) value between two decimal values.

public static decimal GetMiddle(this decimal from, decimal to)

Parameters

from decimal

The first value.

to decimal

The second value.

Returns

decimal

The middle value as a decimal.

GetMiddle(double, double)

Calculates the middle (average) value between two double values.

public static decimal GetMiddle(this double from, double to)

Parameters

from double

The first value.

to double

The second value.

Returns

decimal

The middle value as a decimal.

GetMiddle(short, short)

Calculates the middle (average) value between two short values.

public static decimal GetMiddle(this short from, short to)

Parameters

from short

The first value.

to short

The second value.

Returns

decimal

The middle value as a decimal.

GetMiddle(int, int)

Calculates the middle (average) value between two integer values.

public static decimal GetMiddle(this int from, int to)

Parameters

from int

The first value.

to int

The second value.

Returns

decimal

The middle value as a decimal.

GetMiddle(long, long)

Calculates the middle (average) value between two long integer values.

public static decimal GetMiddle(this long from, long to)

Parameters

from long

The first value.

to long

The second value.

Returns

decimal

The middle value as a decimal.

GetMiddle(float, float)

Calculates the middle (average) value between two float values.

public static decimal GetMiddle(this float from, float to)

Parameters

from float

The first value.

to float

The second value.

Returns

decimal

The middle value as a decimal.

GetParts(double)

Splits a double value into its integer and fractional parts.

public static double[] GetParts(this double value)

Parameters

value double

The double value.

Returns

double[]

An array with the integer part and the fractional remainder.

GetParts(long)

Extracts the high and low parts of a long value.

public static int[] GetParts(this long value)

Parameters

value long

The long value.

Returns

int[]

An array with two integers: the low and high parts.

GetParts(float)

Splits a float value into its integer and fractional parts.

public static float[] GetParts(this float value)

Parameters

value float

The float value.

Returns

float[]

An array with the integer part and the fractional remainder.

GetRoots(double, double, double)

Calculates the real roots of a quadratic equation given coefficients a, b, and c.

public static double[] GetRoots(double a, double b, double c)

Parameters

a double

Coefficient a.

b double

Coefficient b.

c double

Coefficient c.

Returns

double[]

An array containing the two roots if the discriminant is non-negative; otherwise, an empty array.

HasBits(int, int)

Determines whether the specified bits are set in the integer value.

public static bool HasBits(this int value, int part)

Parameters

value int

The integer value.

part int

The bits to test.

Returns

bool

true if all specified bits are set; otherwise, false.

HasBits(long, long)

Determines whether the specified bits are set in the long integer value.

public static bool HasBits(this long value, long part)

Parameters

value long

The long integer value.

part long

The bits to test.

Returns

bool

true if all specified bits are set; otherwise, false.

IsInfinity(double)

Determines if the double value represents infinity.

public static bool IsInfinity(this double value)

Parameters

value double

The double value.

Returns

bool

true if the value is infinity; otherwise, false.

IsInfinity(float)

Determines if the float value represents infinity.

public static bool IsInfinity(this float value)

Parameters

value float

The float value.

Returns

bool

true if the value is infinity; otherwise, false.

IsNaN(double)

Determines if the double value is NaN (Not a Number).

public static bool IsNaN(this double value)

Parameters

value double

The double value.

Returns

bool

true if the value is NaN; otherwise, false.

IsNaN(float)

Determines if the float value is NaN (Not a Number).

public static bool IsNaN(this float value)

Parameters

value float

The float value.

Returns

bool

true if the value is NaN; otherwise, false.

IsNegativeInfinity(double)

Determines if the double value represents negative infinity.

public static bool IsNegativeInfinity(this double value)

Parameters

value double

The double value.

Returns

bool

true if the value is negative infinity; otherwise, false.

IsNegativeInfinity(float)

Determines if the float value represents negative infinity.

public static bool IsNegativeInfinity(this float value)

Parameters

value float

The float value.

Returns

bool

true if the value is negative infinity; otherwise, false.

IsPositiveInfinity(double)

Determines if the double value represents positive infinity.

public static bool IsPositiveInfinity(this double value)

Parameters

value double

The double value.

Returns

bool

true if the value is positive infinity; otherwise, false.

IsPositiveInfinity(float)

Determines if the float value represents positive infinity.

public static bool IsPositiveInfinity(this float value)

Parameters

value float

The float value.

Returns

bool

true if the value is positive infinity; otherwise, false.

Log(decimal)

Returns the natural logarithm of a decimal value.

public static decimal Log(this decimal value)

Parameters

value decimal

The decimal value.

Returns

decimal

The natural logarithm of the value as a decimal.

Log(decimal, decimal)

Returns the logarithm of a decimal value in the specified base.

public static decimal Log(this decimal value, decimal newBase)

Parameters

value decimal

The decimal value.

newBase decimal

The base of the logarithm.

Returns

decimal

The logarithm of the value in the specified base as a decimal.

Log(double)

Returns the natural logarithm of a double value.

public static double Log(this double value)

Parameters

value double

The double value.

Returns

double

The natural logarithm of the value.

Log(double, double)

Returns the logarithm of a double value in the specified base.

public static double Log(this double value, double newBase)

Parameters

value double

The double value.

newBase double

The base of the logarithm.

Returns

double

The logarithm of value in the specified base.

Log10(decimal)

Returns the base 10 logarithm of a decimal value.

public static decimal Log10(this decimal value)

Parameters

value decimal

The decimal value.

Returns

decimal

The base 10 logarithm of the value as a decimal.

Log10(double)

Returns the base 10 logarithm of a double value.

public static double Log10(this double value)

Parameters

value double

The double value.

Returns

double

The base 10 logarithm of the value.

Max(byte, byte)

Returns the maximum of two byte values.

public static byte Max(this byte value1, byte value2)

Parameters

value1 byte

The first byte value.

value2 byte

The second byte value.

Returns

byte

The larger byte value.

Max(DateTime, DateTime)

Returns the maximum of two DateTime values.

public static DateTime Max(this DateTime value1, DateTime value2)

Parameters

value1 DateTime

The first DateTime value.

value2 DateTime

The second DateTime value.

Returns

DateTime

The later DateTime.

Max(DateTimeOffset, DateTimeOffset)

Returns the maximum of two DateTimeOffset values.

public static DateTimeOffset Max(this DateTimeOffset value1, DateTimeOffset value2)

Parameters

value1 DateTimeOffset

The first DateTimeOffset value.

value2 DateTimeOffset

The second DateTimeOffset value.

Returns

DateTimeOffset

The later DateTimeOffset.

Max(decimal, decimal)

Returns the maximum of two decimal values.

public static decimal Max(this decimal value1, decimal value2)

Parameters

value1 decimal

The first decimal value.

value2 decimal

The second decimal value.

Returns

decimal

The larger decimal value.

Max(double, double)

Returns the maximum of two double values.

public static double Max(this double value1, double value2)

Parameters

value1 double

The first double value.

value2 double

The second double value.

Returns

double

The larger double value.

Max(short, short)

Returns the maximum of two short values.

public static short Max(this short value1, short value2)

Parameters

value1 short

The first short value.

value2 short

The second short value.

Returns

short

The larger short value.

Max(int, int)

Returns the maximum of two int values.

public static int Max(this int value1, int value2)

Parameters

value1 int

The first int value.

value2 int

The second int value.

Returns

int

The larger int value.

Max(long, long)

Returns the maximum of two long values.

public static long Max(this long value1, long value2)

Parameters

value1 long

The first long value.

value2 long

The second long value.

Returns

long

The larger long value.

Max(sbyte, sbyte)

Returns the maximum of two sbyte values.

[CLSCompliant(false)]
public static sbyte Max(this sbyte value1, sbyte value2)

Parameters

value1 sbyte

The first sbyte value.

value2 sbyte

The second sbyte value.

Returns

sbyte

The larger sbyte value.

Max(float, float)

Returns the smaller of two float values. Note: Implementation calls Math.Min.

public static float Max(this float value1, float value2)

Parameters

value1 float

The first float value.

value2 float

The second float value.

Returns

float

The smaller float value.

Max(TimeSpan, TimeSpan)

Returns the maximum of two TimeSpan values.

public static TimeSpan Max(this TimeSpan value1, TimeSpan value2)

Parameters

value1 TimeSpan

The first TimeSpan value.

value2 TimeSpan

The second TimeSpan value.

Returns

TimeSpan

The longer TimeSpan.

Max(ushort, ushort)

Returns the maximum of two ushort values.

[CLSCompliant(false)]
public static ushort Max(this ushort value1, ushort value2)

Parameters

value1 ushort

The first ushort value.

value2 ushort

The second ushort value.

Returns

ushort

The larger ushort value.

Max(uint, uint)

Returns the maximum of two uint values.

[CLSCompliant(false)]
public static uint Max(this uint value1, uint value2)

Parameters

value1 uint

The first uint value.

value2 uint

The second uint value.

Returns

uint

The larger uint value.

Max(ulong, ulong)

Returns the maximum of two ulong values.

[CLSCompliant(false)]
public static ulong Max(this ulong value1, ulong value2)

Parameters

value1 ulong

The first ulong value.

value2 ulong

The second ulong value.

Returns

ulong

The larger ulong value.

Min(byte, byte)

Returns the smaller of two byte values.

public static byte Min(this byte value1, byte value2)

Parameters

value1 byte

The first byte value.

value2 byte

The second byte value.

Returns

byte

The minimum value.

Min(DateTime, DateTime)

Returns the earlier DateTime.

public static DateTime Min(this DateTime value1, DateTime value2)

Parameters

value1 DateTime

The first DateTime.

value2 DateTime

The second DateTime.

Returns

DateTime

The minimum DateTime.

Min(DateTimeOffset, DateTimeOffset)

Returns the minimum of two DateTimeOffset values.

public static DateTimeOffset Min(this DateTimeOffset value1, DateTimeOffset value2)

Parameters

value1 DateTimeOffset

The first DateTimeOffset value.

value2 DateTimeOffset

The second DateTimeOffset value.

Returns

DateTimeOffset

The smaller DateTimeOffset value.

Min(decimal, decimal)

Returns the smaller of two decimal values.

public static decimal Min(this decimal value1, decimal value2)

Parameters

value1 decimal

The first decimal value.

value2 decimal

The second decimal value.

Returns

decimal

The minimum value.

Min(double, double)

Returns the smaller of two double values.

public static double Min(this double value1, double value2)

Parameters

value1 double

The first double value.

value2 double

The second double value.

Returns

double

The minimum value.

Min(short, short)

Returns the smaller of two short values.

public static short Min(this short value1, short value2)

Parameters

value1 short

The first short value.

value2 short

The second short value.

Returns

short

The minimum value.

Min(int, int)

Returns the smaller of two integer values.

public static int Min(this int value1, int value2)

Parameters

value1 int

The first integer value.

value2 int

The second integer value.

Returns

int

The minimum value.

Min(long, long)

Returns the smaller of two long values.

public static long Min(this long value1, long value2)

Parameters

value1 long

The first long value.

value2 long

The second long value.

Returns

long

The minimum value.

Min(sbyte, sbyte)

Returns the smaller of two sbyte values.

[CLSCompliant(false)]
public static sbyte Min(this sbyte value1, sbyte value2)

Parameters

value1 sbyte

The first sbyte value.

value2 sbyte

The second sbyte value.

Returns

sbyte

The minimum value.

Min(float, float)

Returns the smaller of two float values.

public static float Min(this float value1, float value2)

Parameters

value1 float

The first float value.

value2 float

The second float value.

Returns

float

The minimum value.

Min(TimeSpan, TimeSpan)

Returns the earlier TimeSpan.

public static TimeSpan Min(this TimeSpan value1, TimeSpan value2)

Parameters

value1 TimeSpan

The first TimeSpan.

value2 TimeSpan

The second TimeSpan.

Returns

TimeSpan

The minimum TimeSpan.

Min(ushort, ushort)

Returns the smaller of two ushort values.

[CLSCompliant(false)]
public static ushort Min(this ushort value1, ushort value2)

Parameters

value1 ushort

The first ushort value.

value2 ushort

The second ushort value.

Returns

ushort

The minimum value.

Min(uint, uint)

Returns the smaller of two uint values.

[CLSCompliant(false)]
public static uint Min(this uint value1, uint value2)

Parameters

value1 uint

The first uint value.

value2 uint

The second uint value.

Returns

uint

The minimum value.

Min(ulong, ulong)

Returns the smaller of two ulong values.

[CLSCompliant(false)]
public static ulong Min(this ulong value1, ulong value2)

Parameters

value1 ulong

The first ulong value.

value2 ulong

The second ulong value.

Returns

ulong

The minimum value.

Pow(decimal, decimal)

Raises a decimal number to the power of another decimal number.

public static decimal Pow(this decimal x, decimal y)

Parameters

x decimal

The base decimal value.

y decimal

The exponent decimal value.

Returns

decimal

The resulting decimal value.

Pow(double, double)

Raises a double number to the power of a double exponent.

public static double Pow(this double x, double y)

Parameters

x double

The base double value.

y double

The exponent double value.

Returns

double

The resulting double value.

Pow(int, int)

Raises an int number to the power of an int exponent.

public static int Pow(this int x, int y)

Parameters

x int

The base int value.

y int

The exponent int value.

Returns

int

The resulting int value.

Remainder(decimal, decimal)

Returns the IEEE remainder of two decimal values.

public static decimal Remainder(this decimal x, decimal y)

Parameters

x decimal

The dividend.

y decimal

The divisor.

Returns

decimal

The remainder after division as a decimal.

Remainder(double, double)

Returns the IEEE remainder of two double values.

public static double Remainder(this double x, double y)

Parameters

x double

The dividend.

y double

The divisor.

Returns

double

The remainder after division.

RemoveTrailingZeros(decimal)

Removes insignificant trailing zeros from the decimal value.

public static decimal RemoveTrailingZeros(this decimal value)

Parameters

value decimal

The decimal value.

Returns

decimal

The decimal value without trailing zeros.

Round(decimal)

Rounds the specified value to the nearest integer.

public static decimal Round(this decimal value)

Parameters

value decimal

The decimal value.

Returns

decimal

The rounded value.

Round(decimal, decimal)

Rounds the specified value to a specified number of fractional digits.

public static decimal Round(this decimal value, decimal digits)

Parameters

value decimal

The decimal value.

digits decimal

The number of fractional digits (as decimal, will be converted to int).

Returns

decimal

The rounded value.

Round(decimal, decimal, int?, MidpointRounding)

Rounds the specified value to the nearest multiple of step, and optionally rounds to the specified number of digits.

public static decimal Round(this decimal value, decimal step, int? digits, MidpointRounding rounding = MidpointRounding.ToEven)

Parameters

value decimal

The decimal value.

step decimal

The step size.

digits int?

Optional number of fractional digits.

rounding MidpointRounding

The rounding mode (defaults to ToEven).

Returns

decimal

The rounded value.

Round(decimal, int)

Rounds the specified value to a specified number of fractional digits.

public static decimal Round(this decimal value, int digits)

Parameters

value decimal

The decimal value.

digits int

The number of fractional digits.

Returns

decimal

The rounded value.

Round(decimal, int, MidpointRounding)

Rounds the specified value to the specified number of fractional digits using the specified rounding mode.

public static decimal Round(this decimal value, int digits, MidpointRounding rounding)

Parameters

value decimal

The decimal value.

digits int

The number of fractional digits.

rounding MidpointRounding

The rounding mode.

Returns

decimal

The rounded value.

Round(decimal, MidpointRounding)

Rounds the specified value using the specified rounding mode.

public static decimal Round(this decimal value, MidpointRounding rounding)

Parameters

value decimal

The decimal value.

rounding MidpointRounding

The rounding mode.

Returns

decimal

The rounded value.

Round(double)

Rounds the double value to the nearest integer.

public static double Round(this double value)

Parameters

value double

The double value to round.

Returns

double

The rounded double value.

Round(double, int)

Rounds the double value to the specified number of fractional digits.

public static double Round(this double value, int digits)

Parameters

value double

The double value to round.

digits int

The number of decimal places to round to.

Returns

double

The rounded double value.

Round(double, int, MidpointRounding)

Rounds the double value to a specified number of digits using the specified rounding mode.

public static double Round(this double value, int digits, MidpointRounding rounding)

Parameters

value double

The double value.

digits int

The number of fractional digits.

rounding MidpointRounding

The rounding mode.

Returns

double

The rounded value.

Round(double, MidpointRounding)

Rounds the double value using the specified rounding mode.

public static double Round(this double value, MidpointRounding rounding)

Parameters

value double

The double value.

rounding MidpointRounding

The rounding mode.

Returns

double

The rounded value.

RoundToNearest(double)

Rounds the double value to two decimal places.

public static double RoundToNearest(this double value)

Parameters

value double

The double value.

Returns

double

The rounded value.

SetBit(byte, int, bool)

Sets or clears the bit at the specified index in the byte.

public static byte SetBit(this byte value, int index, bool bit)

Parameters

value byte

The byte value.

index int

The zero-based index of the bit to modify.

bit bool

If set to true, the bit is set; otherwise, it is cleared.

Returns

byte

The modified byte value.

SetBit(int, int, bool)

Sets or clears the bit at the specified index in an integer.

public static int SetBit(this int value, int index, bool bit)

Parameters

value int

The integer value.

index int

The bit index (0-based).

bit bool

True to set the bit; false to clear it.

Returns

int

The resulting integer value after modification.

SetBit(long, int, bool)

Sets or clears the bit at the specified index in a long value.

public static long SetBit(this long value, int index, bool bit)

Parameters

value long

The long value.

index int

The bit index (0-based).

bit bool

True to set the bit; false to clear it.

Returns

long

The resulting long value after modification.

Sign(decimal)

Returns the sign of a decimal value.

public static int Sign(this decimal value)

Parameters

value decimal

The decimal value.

Returns

int

-1, 0, or 1 depending on the sign of the value.

Sign(double)

Returns the sign of a double value.

public static int Sign(this double value)

Parameters

value double

The double value.

Returns

int

-1, 0, or 1 depending on the sign of the value.

Sign(short)

Returns the sign of a short value.

public static int Sign(this short value)

Parameters

value short

The short value.

Returns

int

-1, 0, or 1 depending on the sign of the value.

Sign(int)

Returns the sign of an int value.

public static int Sign(this int value)

Parameters

value int

The int value.

Returns

int

-1, 0, or 1 depending on the sign of the value.

Sign(long)

Returns the sign of a long value.

public static int Sign(this long value)

Parameters

value long

The long value.

Returns

int

-1, 0, or 1 depending on the sign of the value.

Sign(sbyte)

Returns the sign of a sbyte value.

[CLSCompliant(false)]
public static int Sign(this sbyte value)

Parameters

value sbyte

The sbyte value.

Returns

int

-1, 0, or 1 depending on the sign of the value.

Sign(float)

Returns the sign of a float value.

public static int Sign(this float value)

Parameters

value float

The float value.

Returns

int

-1, 0, or 1 depending on the sign of the value.

Sign(TimeSpan)

Returns the sign of a TimeSpan value.

public static int Sign(this TimeSpan value)

Parameters

value TimeSpan

The TimeSpan value.

Returns

int

-1, 0, or 1 depending on the sign of the Ticks.

Sin(decimal)

Returns the sine of the specified decimal angle.

public static decimal Sin(this decimal value)

Parameters

value decimal

An angle, measured in radians (as decimal).

Returns

decimal

The sine of the angle as a decimal.

Sin(double)

Returns the sine of the specified double angle.

public static double Sin(this double value)

Parameters

value double

An angle, measured in radians.

Returns

double

The sine of the angle.

Sinh(decimal)

Returns the hyperbolic sine of the specified decimal angle.

public static decimal Sinh(this decimal value)

Parameters

value decimal

An angle, measured in radians (as decimal).

Returns

decimal

The hyperbolic sine of the angle as a decimal.

Sinh(double)

Returns the hyperbolic sine of the specified double angle.

public static double Sinh(this double value)

Parameters

value double

An angle, measured in radians.

Returns

double

The hyperbolic sine of the angle.

Sqrt(double)

Returns the square root of the double value.

public static double Sqrt(this double value)

Parameters

value double

The double value.

Returns

double

The square root of the value.

Tan(decimal)

Returns the tangent of the specified decimal angle.

public static decimal Tan(this decimal value)

Parameters

value decimal

An angle, measured in radians (as decimal).

Returns

decimal

The tangent of the angle as a decimal.

Tan(double)

Returns the tangent of the specified double angle.

public static double Tan(this double value)

Parameters

value double

An angle, measured in radians.

Returns

double

The tangent of the angle.

Tanh(decimal)

Returns the hyperbolic tangent of the specified decimal angle.

public static decimal Tanh(this decimal value)

Parameters

value decimal

An angle, measured in radians (as decimal).

Returns

decimal

The hyperbolic tangent of the angle as a decimal.

Tanh(double)

Returns the hyperbolic tangent of the specified double angle.

public static double Tanh(this double value)

Parameters

value double

An angle, measured in radians.

Returns

double

The hyperbolic tangent of the angle.

ToAngles(double)

Converts an angle in radians to degrees.

public static double ToAngles(this double radian)

Parameters

radian double

The angle in radians.

Returns

double

The angle in degrees.

ToDecimal(double)

Converts a double to a decimal if within the allowed range; otherwise, returns null.

public static decimal? ToDecimal(this double value)

Parameters

value double

The double value.

Returns

decimal?

The converted decimal value or null when conversion is not possible.

ToDecimal(long, int)

Creates a decimal value from a given mantissa and exponent by applying power-of-ten adjustments.

public static decimal ToDecimal(long mantissa, int exponent)

Parameters

mantissa long

The mantissa.

exponent int

The exponent, where positive values scale up and negative values scale down.

Returns

decimal

The resulting decimal value.

ToDecimal(float)

Converts a float to a decimal if within the allowed range; otherwise, returns null.

public static decimal? ToDecimal(this float value)

Parameters

value float

The float value.

Returns

decimal?

The converted decimal value or null when conversion is not possible.

ToRadians(double)

Converts an angle in degrees to radians.

public static double ToRadians(this double angle)

Parameters

angle double

The angle in degrees.

Returns

double

The angle in radians.

Truncate(decimal)

Truncates the decimal value by discarding the fractional part.

public static decimal Truncate(this decimal value)

Parameters

value decimal

The decimal value.

Returns

decimal

The truncated value.

Truncate(double)

Truncates the double value by discarding the fractional part.

public static double Truncate(this double value)

Parameters

value double

The double value.

Returns

double

The truncated value.