Class MathHelper
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
decimalThe 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
doubleThe 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
shortThe 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
intThe 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
longThe 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
sbyteThe 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
floatThe 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
TimeSpanThe 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
decimalA 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
doubleA 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
doubleThe 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
intThe 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
longThe 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
floatThe 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
decimalA 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
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
doubleA 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
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
decimalA 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
doubleA 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
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
decimalThe 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
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
doubleThe 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
floatThe 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
decimalAn 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
doubleAn 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
decimalAn 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
doubleAn 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
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
longThe dividend.
b
longThe divisor.
result
longWhen 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
decimalA 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
doubleA 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
decimalThe decimal value.
mantissa
longThe extracted mantissa.
exponent
intThe 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
doubleThe double value.
mantissa
longThe extracted mantissa.
exponent
intThe 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
decimalThe 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
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
doubleThe 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
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
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
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
floatThe 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
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
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
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
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
decimalThe 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
decimalThe 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
intThe 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
longThe 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
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
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
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
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
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
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
doubleThe 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
longThe 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
floatThe 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
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
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
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
doubleThe 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
floatThe 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
doubleThe 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
floatThe 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
doubleThe 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
floatThe 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
doubleThe 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
floatThe 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
decimalThe 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
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
doubleThe 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
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
decimalThe 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
doubleThe 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
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
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
DateTimeOffsetThe first DateTimeOffset value.
value2
DateTimeOffsetThe 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
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
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
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
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
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
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
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
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
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
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
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
Returns
- byte
The minimum value.
Min(DateTime, DateTime)
Returns the earlier DateTime.
public static DateTime Min(this DateTime value1, DateTime value2)
Parameters
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
DateTimeOffsetThe first DateTimeOffset value.
value2
DateTimeOffsetThe 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
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
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
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
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
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
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
Returns
- float
The minimum value.
Min(TimeSpan, TimeSpan)
Returns the earlier TimeSpan.
public static TimeSpan Min(this TimeSpan value1, TimeSpan value2)
Parameters
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
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
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
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
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
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
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
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
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
decimalThe 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
decimalThe 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
decimalThe decimal value.
digits
decimalThe 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
decimalThe decimal value.
step
decimalThe step size.
digits
int?Optional number of fractional digits.
rounding
MidpointRoundingThe 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
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
decimalThe decimal value.
digits
intThe number of fractional digits.
rounding
MidpointRoundingThe 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
decimalThe decimal value.
rounding
MidpointRoundingThe 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
doubleThe 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
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
doubleThe double value.
digits
intThe number of fractional digits.
rounding
MidpointRoundingThe 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
doubleThe double value.
rounding
MidpointRoundingThe 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
doubleThe 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
byteThe byte value.
index
intThe zero-based index of the bit to modify.
bit
boolIf 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
intThe integer value.
index
intThe bit index (0-based).
bit
boolTrue 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
longThe long value.
index
intThe bit index (0-based).
bit
boolTrue 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
decimalThe 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
doubleThe 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
shortThe 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
intThe 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
longThe 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
sbyteThe 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
floatThe 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
TimeSpanThe 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
decimalAn 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
doubleAn 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
decimalAn 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
doubleAn 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
doubleThe 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
decimalAn 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
doubleAn 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
decimalAn 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
doubleAn 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
doubleThe 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
doubleThe 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
longThe mantissa.
exponent
intThe 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
floatThe 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
doubleThe 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
decimalThe 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
doubleThe double value.
Returns
- double
The truncated value.