-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* PT-1924: Add rounding policy * PT-1924: Add unit tests * PT-1924: Set Round Policy in CurrencyService * PT-1924: Add RoundType and MidpointRounding properties to the blade * PT-1924: Fix deserialization issue Co-authored-by: Filipp Kuksov <[email protected]>
- Loading branch information
1 parent
4ff78c9
commit 55e7c24
Showing
16 changed files
with
519 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/VirtoCommerce.CoreModule.Core/Currency/DefaultMoneyRoundingPolicy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System; | ||
using VirtoCommerce.CoreModule.Core.Enums; | ||
using VirtoCommerce.Platform.Core.Common; | ||
|
||
namespace VirtoCommerce.CoreModule.Core.Currency | ||
{ | ||
// TODO: Reduce the cognitive complexity | ||
// https://github.com/grandnode/grandnode2/blob/master/src/Business/Grand.Business.Catalog/Extensions/RoundingHelper.cs#L44 | ||
public class DefaultMoneyRoundingPolicy : IMoneyRoundingPolicy | ||
{ | ||
public decimal RoundMoney(decimal amount, Currency currency) | ||
{ | ||
var roundingType = EnumUtility.SafeParse(currency.RoundingType, RoundingType.Rounding001); | ||
var midpointRounding = EnumUtility.SafeParse(currency.MidpointRounding, MidpointRounding.AwayFromZero); | ||
return Round(amount, currency.NumberFormat.NumberDecimalDigits, roundingType, midpointRounding); | ||
} | ||
|
||
/// <summary> | ||
/// Round | ||
/// </summary> | ||
/// <param name="value">Value to round</param> | ||
/// <param name="decimals">Number of digits in fractional part</param> | ||
/// <param name="roundingType">The rounding type</param> | ||
/// <param name="midpointRounding">Specifies how mathematical rounding methods should process a number that is midway between two numbers</param> | ||
/// <returns>Rounded value</returns> | ||
protected decimal Round(decimal value, int decimals, RoundingType roundingType, MidpointRounding midpointRounding) | ||
{ | ||
//default round (Rounding001) | ||
var result = Math.Round(value, decimals, midpointRounding); | ||
var fractionPart = (result - Math.Truncate(result)) * 10; | ||
|
||
//cash rounding not needed | ||
if (fractionPart == 0) | ||
return result; | ||
|
||
//Cash rounding (details: https://en.wikipedia.org/wiki/Cash_rounding) | ||
switch (roundingType) | ||
{ | ||
//rounding with 0.05 or 5 intervals | ||
case RoundingType.Rounding005Up: | ||
case RoundingType.Rounding005Down: | ||
fractionPart = (fractionPart - Math.Truncate(fractionPart)) * 10; | ||
if (fractionPart == 5 || fractionPart == 0) | ||
break; | ||
if (roundingType == RoundingType.Rounding005Down) | ||
fractionPart = fractionPart > 5 ? 5 - fractionPart : fractionPart * -1; | ||
else | ||
fractionPart = fractionPart > 5 ? 10 - fractionPart : 5 - fractionPart; | ||
|
||
result += fractionPart / 100; | ||
break; | ||
//rounding with 0.10 intervals | ||
case RoundingType.Rounding01Up: | ||
case RoundingType.Rounding01Down: | ||
fractionPart = (fractionPart - Math.Truncate(fractionPart)) * 10; | ||
if (fractionPart == 0) | ||
break; | ||
|
||
if (roundingType == RoundingType.Rounding01Down && fractionPart == 5) | ||
fractionPart = -5; | ||
else | ||
fractionPart = fractionPart < 5 ? fractionPart * -1 : 10 - fractionPart; | ||
|
||
result += fractionPart / 100; | ||
break; | ||
//rounding with 0.50 intervals | ||
case RoundingType.Rounding05: | ||
fractionPart *= 10; | ||
fractionPart = fractionPart < 25 ? fractionPart * -1 : fractionPart < 50 || fractionPart < 75 ? 50 - fractionPart : 100 - fractionPart; | ||
|
||
result += fractionPart / 100; | ||
break; | ||
//rounding with 1.00 intervals | ||
case RoundingType.Rounding1: | ||
case RoundingType.Rounding1Up: | ||
fractionPart *= 10; | ||
|
||
if (roundingType == RoundingType.Rounding1Up && fractionPart > 0) | ||
result = Math.Truncate(result) + 1; | ||
else | ||
result = fractionPart < 50 ? Math.Truncate(result) : Math.Truncate(result) + 1; | ||
|
||
break; | ||
case RoundingType.Rounding001: | ||
default: | ||
break; | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/VirtoCommerce.CoreModule.Core/Currency/IMoneyRoundingPolicy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace VirtoCommerce.CoreModule.Core.Currency | ||
{ | ||
public interface IMoneyRoundingPolicy | ||
{ | ||
decimal RoundMoney(decimal amount, Currency currency); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace VirtoCommerce.CoreModule.Core.Enums | ||
{ | ||
//Cash rounding (details: https://en.wikipedia.org/wiki/Cash_rounding) | ||
public enum RoundingType | ||
{ | ||
Rounding001, | ||
Rounding005Up, | ||
Rounding005Down, | ||
Rounding01Up, | ||
Rounding01Down, | ||
Rounding05, | ||
Rounding1, | ||
Rounding1Up | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.