From 50e4d0168c2d5ef6439b0fcf25c6400626bd5321 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ph=E1=BA=A1m=20H=E1=BB=93ng=20Ph=C3=BAc?= Date: Fri, 16 Aug 2024 14:10:32 +0700 Subject: [PATCH] [BDB-12] Fix double to string culture difference (#14) --- .../Implementations/NumberMustBeGreaterThan.cs | 4 +++- .../Shared/Utils/DoubleUtility.cs | 13 +++++++++++++ .../Utils}/ValueObjectUtility.cs | 0 .../Domain/Shared/Utils/DoubleUtilityTest.cs | 18 ++++++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 Core/BuildingBlock.Core.Domain/Shared/Utils/DoubleUtility.cs rename Core/BuildingBlock.Core.Domain/{ValueObjects => Shared/Utils}/ValueObjectUtility.cs (100%) create mode 100644 Tests/BuildingBlock.Test.UnitTest/Core/Domain/Shared/Utils/DoubleUtilityTest.cs diff --git a/Core/BuildingBlock.Core.Domain/Rules/Implementations/NumberMustBeGreaterThan.cs b/Core/BuildingBlock.Core.Domain/Rules/Implementations/NumberMustBeGreaterThan.cs index 3e13ec7..60f22d1 100644 --- a/Core/BuildingBlock.Core.Domain/Rules/Implementations/NumberMustBeGreaterThan.cs +++ b/Core/BuildingBlock.Core.Domain/Rules/Implementations/NumberMustBeGreaterThan.cs @@ -1,4 +1,5 @@ using BuildingBlock.Core.Domain.Rules.Abstractions; +using BuildingBlock.Core.Domain.Shared.Utils; namespace BuildingBlock.Core.Domain.Rules.Implementations; @@ -45,7 +46,8 @@ public bool IsBroken() var valueName = _valueName is not null ? $"{_valueName}: " : string.Empty; var compareValueName = _compareValueName is not null ? $"{_compareValueName}: " : string.Empty; - Message = $"{valueName}{_value} must be greater than {compareValueName}{_compareValue}."; + Message = + $"{valueName}{DoubleUtility.ToString(Convert.ToDouble(_value))} must be greater than {compareValueName}{DoubleUtility.ToString(Convert.ToDouble(_compareValue))}."; return true; } diff --git a/Core/BuildingBlock.Core.Domain/Shared/Utils/DoubleUtility.cs b/Core/BuildingBlock.Core.Domain/Shared/Utils/DoubleUtility.cs new file mode 100644 index 0000000..ca1440a --- /dev/null +++ b/Core/BuildingBlock.Core.Domain/Shared/Utils/DoubleUtility.cs @@ -0,0 +1,13 @@ +using System.Globalization; + +namespace BuildingBlock.Core.Domain.Shared.Utils; + +public static class DoubleUtility +{ + public static string ToString(double value) + { + var valueString = value.ToString(CultureInfo.InvariantCulture); + + return valueString.Replace('.', ','); + } +} \ No newline at end of file diff --git a/Core/BuildingBlock.Core.Domain/ValueObjects/ValueObjectUtility.cs b/Core/BuildingBlock.Core.Domain/Shared/Utils/ValueObjectUtility.cs similarity index 100% rename from Core/BuildingBlock.Core.Domain/ValueObjects/ValueObjectUtility.cs rename to Core/BuildingBlock.Core.Domain/Shared/Utils/ValueObjectUtility.cs diff --git a/Tests/BuildingBlock.Test.UnitTest/Core/Domain/Shared/Utils/DoubleUtilityTest.cs b/Tests/BuildingBlock.Test.UnitTest/Core/Domain/Shared/Utils/DoubleUtilityTest.cs new file mode 100644 index 0000000..4f683e5 --- /dev/null +++ b/Tests/BuildingBlock.Test.UnitTest/Core/Domain/Shared/Utils/DoubleUtilityTest.cs @@ -0,0 +1,18 @@ +using BuildingBlock.Core.Domain.Shared.Utils; +using FluentAssertions; + +namespace Tests.Core.Domain.Shared.Utils; + +public class DoubleUtilityTest +{ + public class ToString + { + [Fact] + public void ShouldConvertDoubleWithDot() + { + var actualData = DoubleUtility.ToString(1.1); + + actualData.Should().Be("1,1"); + } + } +} \ No newline at end of file