From 4094670f6f3e52b323fcc794d6092df87678c1ad Mon Sep 17 00:00:00 2001 From: lindexi Date: Wed, 7 Feb 2024 16:38:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=99=BE=E5=88=86=E6=AF=94?= =?UTF-8?q?=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Close #42 --- .../Percentage.cs | 5 +++-- .../PercentageTest.cs | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/dotnetCampus.OpenXmlUnitConverter/Percentage.cs b/src/dotnetCampus.OpenXmlUnitConverter/Percentage.cs index 7ffa2f5..b7a3c53 100644 --- a/src/dotnetCampus.OpenXmlUnitConverter/Percentage.cs +++ b/src/dotnetCampus.OpenXmlUnitConverter/Percentage.cs @@ -178,7 +178,8 @@ public override int GetHashCode() /// public static Percentage operator *(Percentage a, Percentage b) { - return new Percentage(a.IntValue * b.IntValue); + // 相乘 和 相除 都要考虑倍率,不如直接靠 DoubleValue 来计算 + return FromDouble(a.DoubleValue * b.DoubleValue); } /// @@ -191,7 +192,7 @@ public override int GetHashCode() { if (b.IntValue == 0) throw new DivideByZeroException(); - return new Percentage(a.IntValue / b.IntValue); + return FromDouble(a.DoubleValue / b.DoubleValue); } /// diff --git a/tests/dotnetCampus.OpenXmlUnitConverter.Tests/PercentageTest.cs b/tests/dotnetCampus.OpenXmlUnitConverter.Tests/PercentageTest.cs index 46f4fd5..196793e 100644 --- a/tests/dotnetCampus.OpenXmlUnitConverter.Tests/PercentageTest.cs +++ b/tests/dotnetCampus.OpenXmlUnitConverter.Tests/PercentageTest.cs @@ -1,4 +1,7 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + using MSTest.Extensions.Contracts; namespace dotnetCampus.OpenXmlUnitConverter.Tests @@ -23,5 +26,17 @@ public void ParsePercentageText() Assert.AreEqual(99999, percentage.IntValue); }); } + + [ContractTestCase] + public void TestCalculate() + { + "非零百分比除以自己等于 100%".Test((double value) => + { + var percentage = Percentage.FromDouble(value); + + var result = percentage / percentage; + Assert.AreEqual(true, Math.Abs(result.DoubleValue - 1) < 0.000001); + }).WithArguments(1.5, 2.3, 3.6, 100.5, 100000.123); + } } }