Skip to content

Number Extensions

Adrian Gabor edited this page Nov 30, 2021 · 11 revisions

TryParseExt

Description
Maps to int.TryParse
Depends on C# 7.2 or later

Parameters
input (string)

Usage

string input = "1";
int output = 0;
output.TryParseExt(input);

ParseExt

Description
Maps to int.Parse
Depends on C# 7.2 or later

Parameters
input (string)

Usage

string input = "1";
int output = 0;
output.ParseExt(input);

ToDateTimeExt

Description
Returns a DateTime object from ticks.

Parameters
None

Usage

long ticks = 123456789;
DateTime d = ticks.ToDateTimeExt();

IsNullOrLessThanOneExt

Description
Checks if a nullable number or byte is null or is less than one.
Types available: int, int16, int64, uint, uint16, uint64, double, single, decimal, byte, sbyte

Parameters
None

Usage

int? i = null;
bool b = i.IsNullOrLessThanOneExt();

IsNullOrZeroExt

Description
Checks if a nullable number or byte is null or equals zero.
Types available: int, int16, int64, uint, uint16, uint64, double, single, decimal, byte, sbyte

Parameters
None

Usage

int? i = 0;
bool b = i.IsNullOrZeroExt();

IsEvenExt

Description
Returns true if a number is even.
Types available: int, double

Parameters
None

Usage

int i = 2;
if (i.IsEvenExt()) { ... }

IsOddExt

Description
Returns true if a number is odd.
Types available: int, double

Parameters
None

Usage

int i = 3;
if (i.IsOddExt()) { ... }

IsIntegralExt

Description
Returns a boolean value indicating if number is an integral value.
Types available: double, decimal

Parameters
None

Usage

double dou = 2.0;
if (dou.IsIntegralExt()) { ... }

ToCurrencyExt

Description
Converts number to a currency string using "C2" formatter.
CultureInfo set to CurrentCulture.
Types available: double, decimal, float, int

Parameters
None

Usage

double d = 1.99;
string currencyString = d.ToCurrencyExt();

Result equals "$1.99" (if in the USA)


ToPercentExt

Description
Converts number to a percent string.
Optional parameter specifies how many decimals out return. Default value is 2 decimal places.
Will round value up or down when necessary.
CultureInfo set to CurrentCulture.
Types available: double, decimal, float, int

Parameters
decimals (int)

Usage

decimal dec = .23M;
string percentString = dec.ToPercentExt(0);

Result equals "23%" (if in the USA)


ToRangeExt

Description
Creates a range of integers starting with the extended type through the count parameter.
Maps to Enumerable.Range

Parameters
count (int)

Usage

int num = 0;
int count = 100;
IEnumerable<int> range = num.ToRangeExt(count);

Result equals "23%" (if in the USA)


ShuffleExt

Description
Rearranges the individual numbers in an integer.

Parameters
None

Usage

int number = 123456789;
int shuffled = number.ShuffleExt();

ToListExt

Description
Creates a list of integers from the individual numbers in the extended int.

Parameters
None

Usage

int number = 123456789;
List<int> numbers = number.ToListExt();

ToInt64BitsExt

Description
Converts the extended double to a 64 bit signed integer (long).
Maps to BitConverter.DoubleToInt64Bits

Parameters
None

Usage

double value = 123.456;
double int64Bits = value.ToInt64BitsExt();

IsGreaterThanExt (Int)

Description
Compares the extended integer to an array of integers.
Returns true if the extended integer is greater than all the integers in the array and false if the extended integer is less than or equal to all the integers in the array.

Parameters
comparer (int[])

Usage

int i = 5;
int[] arr1 = { 1, 2, 3, 4 };
if (i.IsGreaterThanExt(arr1))
{
    // This is true!
}

IsGreaterThanExt (Double)

Description
Compares the extended double to an array of doubles.
Returns true if the extended double is greater than all the doubles in the array and false if the extended double is less than or equal to all the doubles in the array.

Parameters
comparer (double[])

Usage

double d = 5.3;
double[] arr1 = { 1.1, 2.5, 3.7, 4.9 };
if (d.IsGreaterThanExt(arr1))
{
    // This is true!
}

IsGreaterThanExt (Decimal)

Description
Compares the extended decimal to an array of decimals.
Returns true if the extended decimal is greater than all the decimals in the array and false if the extended decimal is less than or equal to all the decimals in the array.

Parameters
comparer (decimal[])

Usage

decimal dec = 5.3M;
decimal[] arr1 = { 1.1M, 2.5M, 3.7M, 4.9M };
if (dec.IsGreaterThanExt(arr1))
{
    // This is true!
}

IsGreaterThanEqualToExt (Int)

Description
Compares the extended integer to an array of integers.
Returns true if the extended integer is greater than or equal to all the integers in the array and false if the extended integer is less than all the integers in the array.

Parameters
comparer (int[])

Usage

int i = 5;
int[] arr1 = { 1, 2, 3, 4, 5 };
if (i.IsGreaterThanEqualToExt(arr1))
{
    // This is true!
}

IsGreaterThanEqualToExt (Double)

Description
Compares the extended double to an array of doubles.
Returns true if the extended double is greater than or equal to all the doubles in the array and false if the extended double is less than all the doubles in the array.

Parameters
comparer (double[])

Usage

double d = 5.3;
double[] arr1 = { 1.1, 2.5, 3.7, 4.9, 5.3 };
if (d.IsGreaterThanEqualToExt(arr1))
{
    // This is true!
}

IsGreaterThanEqualToExt (Decimal)

Description
Compares the extended decimal to an array of decimals.
Returns true if the extended decimal is greater than or equal to all the decimals in the array and false if the extended decimal is less than all the decimals in the array.

Parameters
comparer (decimal[])

Usage

decimal dec = 5.3M;
decimal[] arr1 = { 1.1M, 2.5M, 3.7M, 4.9M, 5.3M };
if (dec.IsGreaterThanEqualToExt(arr1))
{
    // This is true!
}

IsLessThanExt (Int)

Description
Compares the extended integer to an array of integers.
Returns true if the extended integer is less than all the integers in the array and false if the extended integer is greater than or equal to all the integers in the array.

Parameters
comparer (int[])

Usage

int i = 1;
int[] arr1 = { 2, 3, 4, 5 };
if (i.IsLessThanExt(arr1))
{
    // This is true!
}

IsLessThanExt (Double)

Description
Compares the extended double to an array of doubles.
Returns true if the extended double is less than all the doubles in the array and false if the extended double is greater than or equal to all the doubles in the array.

Parameters
comparer (double[])

Usage

double d = 1.1;
double[] arr1 = { 2.5, 3.7, 4.9, 5.3 };
if (d.IsLessThanExt(arr1))
{
    // This is true!
}

IsLessThanExt (Decimal)

Description
Compares the extended decimal to an array of decimals.
Returns true if the extended decimal is less than all the decimals in the array and false if the extended decimal is greater than or equal to all the decimals in the array.

Parameters
comparer (decimal[])

Usage

decimal dec = 1.1M;
decimal[] arr1 = { 2.5M, 3.7M, 4.9M, 5.3M };
if (dec.IsLessThanExt(arr1))
{
    // This is true!
}

IsLessThanEqualToExt (Int)

Description
Compares the extended integer to an array of integers.
Returns true if the extended integer is less than or equal to all the integers in the array and false if the extended integer is greater than all the integers in the array.

Parameters
comparer (int[])

Usage

int i = 1;
int[] arr1 = { 1, 2, 3, 4, 5 };
if (i.IsLessThanEqualToExt(arr1))
{
    // This is true!
}

IsLessThanEqualToExt (Double)

Description
Compares the extended double to an array of doubles.
Returns true if the extended double is less than or equal to all the doubles in the array and false if the extended double is less than all the doubles in the array.

Parameters
comparer (double[])

Usage

double d = 1.1;
double[] arr1 = { 1.1, 2.5, 3.7, 4.9, 5.3 };
if (d.IsLessThanExt(arr1))
{
    // This is true!
}

IsLessThanEqualToExt (Decimal)

Description
Compares the extended decimal to an array of decimals.
Returns true if the extended decimal is less than or equal to all the decimals in the array and false if the extended decimal is less than all the decimals in the array.

Parameters
comparer (decimal[])

Usage

decimal dec = 1.1M;
decimal[] arr1 = { 1.1M, 2.5M, 3.7M, 4.9M, 5.3M };
if (dec.IsLessThanExt(arr1))
{
    // This is true!
}

Clone this wiki locally