Skip to content

Int Extensions

Andrzej Kebab edited this page Feb 4, 2024 · 2 revisions

UtilityLibrary.Core

This utility library provides extension methods for integer values to perform various operations.

PercentageOf

Calculates the percentage of a int value relative to another int value.

int part = // initialize your int value;
int whole = // initialize your int value;

float percentage = part.PercentageOf(whole);

Parameters:

  • part: The int value for which the percentage is calculated.
  • whole: The int value representing the whole or total.

Returns:

  • percentage: A float value representing the percentage of the part relative to the whole.

Clamp

ClampMin0

Clamps the integer value to a minimum of 0.

using UtilityLibrary.Core;

int value = -5;
int result = value.ClampMin0();

Parameters

  • value (int): The integer value to clamp.

Returns

  • int: The clamped value with a minimum of 0.

ClampMin

Clamps the integer value to a specified minimum.

using UtilityLibrary.Core;

int value = -5;
int min = 0;
int result = value.ClampMin(min);

Parameters

  • value (int): The integer value to clamp.
  • min (int): The minimum value to clamp to.

Returns

  • int: The clamped value with a minimum of the specified value.

ClampMax0

Clamps the integer value to a maximum of 0.

using UtilityLibrary.Core;

int value = 5;
int result = value.ClampMax0();

Parameters

  • value (int): The integer value to clamp.

Returns

  • int: The clamped value with a maximum of 0.

ClampMax

Clamps the integer value to a specified maximum.

using UtilityLibrary.Core;

int value = 15;
int max = 10;
int result = value.ClampMax(max);

Parameters

  • value (int): The integer value to clamp.
  • max (int): The maximum value to clamp to.

Returns

  • int: The clamped value with a maximum of the specified value.

Clamp01

Clamps the integer value to the range [0, 1].

using UtilityLibrary.Core;

int value = 5;
int result = value.Clamp01();

Parameters

  • value (int): The integer value to clamp.

Returns

  • int: The clamped value within the range [0, 1].

Clamp

Clamps the integer value within a specified range.

using UtilityLibrary.Core;

int value = 15;
int min = 10;
int max = 20;
int result = value.Clamp(min, max);

Parameters

  • value (int): The integer value to clamp.
  • min (int): The minimum value of the range.
  • max (int): The maximum value of the range.

Returns

  • int: The clamped value within the specified range.

Abs

Abs

Returns the absolute value of the given integer value.

using UtilityLibrary.Core;

int value = -5;
int absoluteValue = value.Abs();

Parameters

  • value (int): The integer value to calculate the absolute value for.

Returns

  • int: The absolute value of the integer value.

Abs (IEnumerable)

Returns a sequence of absolute values for each integer value in the given enumerable.

using UtilityLibrary.Core;

IEnumerable<int> values = new List<int> { -5, 10, -3 };
IEnumerable<int> absoluteValues = values.Abs();

Parameters

  • values (IEnumerable): The enumerable containing integer values.

Returns

  • IEnumerable: A sequence of absolute values for each integer value in the input enumerable.

Range

IsInRange

Determines whether an integer value is within a specified range.

using UtilityLibrary.Core;

int value = 15;
int minValue = 10;
int maxValue = 20;
bool isInRange = value.IsInRange(minValue, maxValue);

Parameters

  • value (int): The integer value to check.
  • min (int): The minimum value of the range.
  • max (int): The maximum value of the range.

Returns

  • bool: True if the integer value is within the range; otherwise, False.

InRange

Returns the integer value if it is within a specified range; otherwise, returns a default value.

using UtilityLibrary.Core;

int value = 25;
int minValue = 10;
int maxValue = 20;
int defaultValue = -1;
int result = value.InRange(minValue, maxValue, defaultValue);

Parameters

  • value (int): The integer value to check.
  • min (int): The minimum value of the range.
  • max (int): The maximum value of the range.
  • defaultValue (int): The default value to return if the integer value is not within the range.

Returns

  • int: The integer value if it is within the range; otherwise, the default value.

Odd / Even

IsEven

Determines whether an integer value is an even number.

using UtilityLibrary.Core;

int value = 10;
bool isEven = value.IsEven();

Parameters

  • value (int): The integer value to check.

Returns

  • bool: True if the integer value is an even number; otherwise, False.

IsOdd

Determines whether an integer value is an odd number.

using UtilityLibrary.Core;

int value = 5;
bool isOdd = value.IsOdd();

Parameters

  • value (int): The integer value to check.

Returns

  • bool: True if the integer value is an odd number; otherwise, False.

Index

GetArrayIndex

Returns the array index corresponding to the integer value.

using UtilityLibrary.Core;

int value = 3;
int arrayIndex = value.GetArrayIndex();

Parameters

  • value (int): The integer value.

Returns

  • int: The array index. If the integer value is 0, returns 0; otherwise, returns the integer value.

IsIndexInArray

Determines whether an integer value is a valid index in a specified array.

using UtilityLibrary.Core;

int value = 3;
Array arrayToCheck = new int[5];
bool isValidIndex = value.IsIndexInArray(arrayToCheck);

Parameters

  • value (int): The integer value to check.
  • arrayToCheck (Array): The array in which to check the index.

Returns

  • bool: True if the integer value is a valid index in the array; otherwise, False.

Clone this wiki locally