From b0dd10cbd0aa6c80b212789390f91bc72303a4e0 Mon Sep 17 00:00:00 2001 From: Artem Dudarev Date: Fri, 15 Nov 2024 15:19:31 +0200 Subject: [PATCH 1/3] fix: Use CurrencyDecimalDigits instead of NumberDecimalDigits when rounding money --- .../Currency/DefaultMoneyRoundingPolicy.cs | 2 +- .../DefaultMoneyRoundingPolicyTests.cs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/VirtoCommerce.CoreModule.Core/Currency/DefaultMoneyRoundingPolicy.cs b/src/VirtoCommerce.CoreModule.Core/Currency/DefaultMoneyRoundingPolicy.cs index 14d30517..1901cbf0 100644 --- a/src/VirtoCommerce.CoreModule.Core/Currency/DefaultMoneyRoundingPolicy.cs +++ b/src/VirtoCommerce.CoreModule.Core/Currency/DefaultMoneyRoundingPolicy.cs @@ -12,7 +12,7 @@ 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); + return Round(amount, currency.NumberFormat.CurrencyDecimalDigits, roundingType, midpointRounding); } /// diff --git a/tests/VirtoCommerce.CoreModule.Tests/DefaultMoneyRoundingPolicyTests.cs b/tests/VirtoCommerce.CoreModule.Tests/DefaultMoneyRoundingPolicyTests.cs index 4174ac71..55415b39 100644 --- a/tests/VirtoCommerce.CoreModule.Tests/DefaultMoneyRoundingPolicyTests.cs +++ b/tests/VirtoCommerce.CoreModule.Tests/DefaultMoneyRoundingPolicyTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using VirtoCommerce.CoreModule.Core.Common; using VirtoCommerce.CoreModule.Core.Currency; using VirtoCommerce.CoreModule.Core.Enums; using Xunit; @@ -14,7 +15,7 @@ public void CanRound(decimal amount, decimal expected, RoundingType roundingType { // Arrange var roundPolicy = new DefaultMoneyRoundingPolicy(); - var currency = new Currency(); + var currency = new Currency(new Language("en-US"), code: null); currency.RoundingType = roundingType.ToString(); currency.MidpointRounding = midpointRounding.ToString(); From 41b932be255259bf7514439801c0f70738ed5e71 Mon Sep 17 00:00:00 2001 From: Artem Dudarev Date: Fri, 15 Nov 2024 15:22:23 +0200 Subject: [PATCH 2/3] fix: Make the multiplication of Money by Money obsolete, as it makes no sense --- src/VirtoCommerce.CoreModule.Core/Currency/Money.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/VirtoCommerce.CoreModule.Core/Currency/Money.cs b/src/VirtoCommerce.CoreModule.Core/Currency/Money.cs index edea9302..4d779988 100644 --- a/src/VirtoCommerce.CoreModule.Core/Currency/Money.cs +++ b/src/VirtoCommerce.CoreModule.Core/Currency/Money.cs @@ -115,6 +115,7 @@ public decimal TruncatedAmount public static Money operator -(Money first, Money second) => new Money(first.InternalAmount - second.ConvertTo(first.Currency).InternalAmount, first.Currency); + [Obsolete("Multiplying Money by Money makes no sense", DiagnosticId = "VC0009", UrlFormat = "https://docs.virtocommerce.org/products/products-virto3-versions/")] public static Money operator *(Money first, Money second) => new Money(first.InternalAmount * second.ConvertTo(first.Currency).InternalAmount, first.Currency); From 9af9116970d141d81ba07aae558f48ebdac3a977 Mon Sep 17 00:00:00 2001 From: Artem Dudarev Date: Fri, 15 Nov 2024 15:23:32 +0200 Subject: [PATCH 3/3] fix: Change return type to decimal when dividing Money by Money --- src/VirtoCommerce.CoreModule.Core/Currency/Money.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/VirtoCommerce.CoreModule.Core/Currency/Money.cs b/src/VirtoCommerce.CoreModule.Core/Currency/Money.cs index 4d779988..c4a7b831 100644 --- a/src/VirtoCommerce.CoreModule.Core/Currency/Money.cs +++ b/src/VirtoCommerce.CoreModule.Core/Currency/Money.cs @@ -119,8 +119,8 @@ public decimal TruncatedAmount public static Money operator *(Money first, Money second) => new Money(first.InternalAmount * second.ConvertTo(first.Currency).InternalAmount, first.Currency); - public static Money operator /(Money first, Money second) - => new Money(first.InternalAmount / second.ConvertTo(first.Currency).InternalAmount, first.Currency); + public static decimal operator /(Money first, Money second) + => first.InternalAmount / second.ConvertTo(first.Currency).InternalAmount; public static bool operator ==(Money money, long value) => !(money is null) && money.InternalAmount == value;