Class DecimalPriceExtensions
- Namespace
- HeroKit.Decimals
- Assembly
- HeroKit.dll
Provides price rounding, percentage and VAT helpers for decimal values.
Methods return unrounded values unless a decimals argument is given.
Callers round money with Math.Round(x, 2, MidpointRounding.AwayFromZero).
public static class DecimalPriceExtensions
- Inheritance
-
DecimalPriceExtensions
- Inherited Members
Methods
AddPercent(decimal, decimal)
Adds a percentage to a value: value * (1 + percent / 100). The result is not rounded.
public static decimal AddPercent(this decimal value, decimal percent)
Parameters
Returns
- decimal
The increased value.
Examples
200m.AddPercent(10m); // 220
ExcludeVat(decimal, decimal, int?)
Removes VAT from a gross amount: gross / (1 + rate / 100).
public static decimal ExcludeVat(this decimal grossAmount, decimal ratePercent, int? decimals = null)
Parameters
grossAmountdecimalThe gross amount.
ratePercentdecimalThe VAT rate in percent. Must not be negative.
decimalsint?When given, rounds the result to this many decimals with AwayFromZero. Otherwise the result is not rounded.
Returns
- decimal
The net amount.
Examples
120m.ExcludeVat(20m); // 100
Exceptions
- ArgumentOutOfRangeException
Thrown when
ratePercentis negative.
IncludeVat(decimal, decimal, int?)
Adds VAT to a net amount: net * (1 + rate / 100).
public static decimal IncludeVat(this decimal netAmount, decimal ratePercent, int? decimals = null)
Parameters
netAmountdecimalThe net amount.
ratePercentdecimalThe VAT rate in percent. Must not be negative.
decimalsint?When given, rounds the result to this many decimals with AwayFromZero. Otherwise the result is not rounded.
Returns
- decimal
The gross amount.
Examples
100m.IncludeVat(20m); // 120
Exceptions
- ArgumentOutOfRangeException
Thrown when
ratePercentis negative.
RoundToStep(decimal, decimal, MidpointRounding)
Rounds a value to the nearest multiple of the step.
public static decimal RoundToStep(this decimal value, decimal step, MidpointRounding mode = MidpointRounding.AwayFromZero)
Parameters
valuedecimalThe value to round.
stepdecimalThe step size. Must be greater than zero.
modeMidpointRoundingThe midpoint rounding mode. The default is AwayFromZero.
Returns
- decimal
The nearest multiple of
step.
Examples
12.37m.RoundToStep(0.05m); // 12.35
2.675m.RoundToStep(0.01m); // 2.68
Exceptions
- ArgumentOutOfRangeException
Thrown when
stepis zero or negative.
SubtractPercent(decimal, decimal)
Subtracts a percentage from a value: value * (1 - percent / 100). The result is not rounded.
public static decimal SubtractPercent(this decimal value, decimal percent)
Parameters
Returns
- decimal
The decreased value.
Examples
200m.SubtractPercent(25m); // 150
ToPsychologicalPrice(decimal, decimal)
Raises a price to the next price that ends with the given fraction (for example 12.99). The result is floor(value) + ending, plus 1 if that is below the value.
public static decimal ToPsychologicalPrice(this decimal value, decimal ending = 0.99)
Parameters
valuedecimalThe price. Must not be negative.
endingdecimalThe fractional ending, from 0 up to but not including 1. The default is 0.99.
Returns
- decimal
The psychological price that is greater than or equal to
value.
Examples
12.30m.ToPsychologicalPrice(); // 12.99
12.60m.ToPsychologicalPrice(0.50m); // 13.50
Exceptions
- ArgumentOutOfRangeException
Thrown when
valueis negative orendingis outside [0, 1).
VatAmountFromGross(decimal, decimal, int?)
Returns the VAT part of a gross amount: gross - ExcludeVat(gross, rate).
public static decimal VatAmountFromGross(this decimal grossAmount, decimal ratePercent, int? decimals = null)
Parameters
grossAmountdecimalThe gross amount.
ratePercentdecimalThe VAT rate in percent. Must not be negative.
decimalsint?When given, rounds the result to this many decimals with AwayFromZero. Otherwise the result is not rounded.
Returns
- decimal
The VAT amount.
Examples
120m.VatAmountFromGross(20m); // 20
Exceptions
- ArgumentOutOfRangeException
Thrown when
ratePercentis negative.