Skip to content

Double Extensions

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

UtilityLibrary.Core

This utility library provides extension methods for double precision floating-point numbers to perform various operations.

PercentageOf

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

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

double percentage = part.PercentageOf(whole);

Parameters:

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

Returns:

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

Abs

Abs

Calculates the absolute value of a double precision floating-point number.

using UtilityLibrary.Core;

double value = -3.14;
double absoluteValue = value.Abs();

Parameters

  • value (double): The double precision floating-point number.

Returns

  • double: The absolute value of the input double precision floating-point number.

Abs (IEnumerable)

Calculates the absolute values of each element in a sequence of double precision floating-point numbers.

using System.Collections.Generic;
using UtilityLibrary.Core;

IEnumerable<double> values = new List<double> { -3.14, 2.71, -1.0 };
IEnumerable<double> absoluteValues = values.Abs();

Parameters

  • value (IEnumerable): The sequence of double precision floating-point numbers.

Returns

  • IEnumerable: The sequence of absolute values.

Round

RoundDecimalPoints

Rounds a double precision floating-point number to a specified number of decimal points.

using UtilityLibrary.Core;

double value = 3.14159;
int decimalPoints = 2;
double roundedValue = value.RoundDecimalPoints(decimalPoints);

Parameters

  • value (double): The double precision floating-point number to round.
  • decimalPoints (int): The number of decimal points to round to.

Returns

  • double: The rounded double precision floating-point number.

RoundToTwoDecimalPoints

Rounds a double precision floating-point number to two decimal points.

using UtilityLibrary.Core;

double value = 3.14159;
double roundedValue = value.RoundToTwoDecimalPoints();

Parameters

  • value (double): The double precision floating-point number to round.

Returns

  • double: The rounded double precision floating-point number.

Range

IsInRange

Determines whether a double precision floating-point number is within a specified range.

using UtilityLibrary.Core;

double value = 5.5;
bool inRange = value.IsInRange(0, 10);

Parameters

  • value (double): The double precision floating-point number to check.
  • minValue (double): The minimum value of the range.
  • maxValue (double): The maximum value of the range.

Returns

  • bool: True if the double precision floating-point number is within the range; otherwise, false.

InRange

Returns a double precision floating-point number if it is within a specified range; otherwise, returns a default value.

using UtilityLibrary.Core;

double value = 15.5;
double result = value.InRange(0, 10, 0);

Parameters

  • value (double): The double precision floating-point number to check.
  • minValue (double): The minimum value of the range.
  • maxValue (double): The maximum value of the range.
  • defaultValue (double): The default value to return if the double precision floating-point number is not within the range.

Returns

  • double: The double precision floating-point number if it is within the range; otherwise, the default value.

TimeSpan

ToDays

Converts a double precision floating-point number to a TimeSpan representing days.

using UtilityLibrary.Core;

double value = 3.5;
TimeSpan timeSpan = value.ToDays();

Parameters

  • value (double): The double precision floating-point number to convert.

Returns

  • TimeSpan: A TimeSpan that represents the specified number of days.

ToHours

Converts a double precision floating-point number to a TimeSpan representing hours.

using UtilityLibrary.Core;

double value = 24.75;
TimeSpan timeSpan = value.ToHours();

Parameters

  • value (double): The double precision floating-point number to convert.

Returns

  • TimeSpan: A TimeSpan that represents the specified number of hours.

ToMinutes

Converts a double precision floating-point number to a TimeSpan representing minutes.

using UtilityLibrary.Core;

double value = 123.45;
TimeSpan timeSpan = value.ToMinutes();

Parameters

  • value (double): The double precision floating-point number to convert.

Returns

  • TimeSpan: A TimeSpan that represents the specified number of minutes.

ToSeconds

Converts a double precision floating-point number to a TimeSpan representing seconds.

using UtilityLibrary.Core;

double value = 987.654;
TimeSpan timeSpan = value.ToSeconds();

Parameters

  • value (double): The double precision floating-point number to convert.

Returns

  • TimeSpan: A TimeSpan that represents the specified number of seconds.

ToMilliseconds

Converts a double precision floating-point number to a TimeSpan representing milliseconds.

using UtilityLibrary.Core;

double value = 1234.5678;
TimeSpan timeSpan = value.ToMilliseconds();

Parameters

  • value (double): The double precision floating-point number to convert.

Returns

  • TimeSpan: A TimeSpan that represents the specified number of milliseconds.

Clone this wiki locally