Skip to content

Decimal Extensions

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

UtilityLibrary.Core

This utility library provides extension methods for decimal numbers to perform various operations.

PercentageOf

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

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

float percentage = part.PercentageOf(whole);

Parameters:

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

Returns:

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

Abs

Abs

Calculates the absolute value of a decimal number.

using UtilityLibrary.Core;

decimal value = -3.14m;
decimal absoluteValue = value.Abs();

Parameters

  • value (decimal): The decimal number.

Returns

  • decimal: The absolute value of the input decimal number.

Abs (IEnumerable)

Calculates the absolute values of each element in a sequence of decimal numbers.

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

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

Parameters

  • value (IEnumerable): The sequence of decimal numbers.

Returns

  • IEnumerable: The sequence of absolute values.

Round

RoundDecimalPoints

Rounds a decimal number to a specified number of decimal points.

using UtilityLibrary.Core;

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

Parameters

  • value (decimal): The decimal number to round.
  • decimalPoints (int): The number of decimal points to round to.

Returns

  • decimal: The rounded decimal number.

RoundToTwoDecimalPoints

Rounds a decimal number to two decimal points.

using UtilityLibrary.Core;

decimal value = 3.14159m;
decimal roundedValue = value.RoundToTwoDecimalPoints();

Parameters

  • value (decimal): The decimal number to round.

Returns

  • decimal: The rounded decimal number.

Clone this wiki locally