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); + } } }