Skip to content

Commit 3a8087c

Browse files
authored
Merge pull request #43 from dotnet-campus/t/lindexi/Percentage
修复百分比计算
2 parents 6602f56 + 4094670 commit 3a8087c

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/dotnetCampus.OpenXmlUnitConverter/Percentage.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ public override int GetHashCode()
178178
/// <returns></returns>
179179
public static Percentage operator *(Percentage a, Percentage b)
180180
{
181-
return new Percentage(a.IntValue * b.IntValue);
181+
// 相乘 和 相除 都要考虑倍率,不如直接靠 DoubleValue 来计算
182+
return FromDouble(a.DoubleValue * b.DoubleValue);
182183
}
183184

184185
/// <summary>
@@ -191,7 +192,7 @@ public override int GetHashCode()
191192
{
192193
if (b.IntValue == 0) throw new DivideByZeroException();
193194

194-
return new Percentage(a.IntValue / b.IntValue);
195+
return FromDouble(a.DoubleValue / b.DoubleValue);
195196
}
196197

197198
/// <summary>

tests/dotnetCampus.OpenXmlUnitConverter.Tests/PercentageTest.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using Microsoft.VisualStudio.TestTools.UnitTesting;
1+
using System;
2+
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
25
using MSTest.Extensions.Contracts;
36

47
namespace dotnetCampus.OpenXmlUnitConverter.Tests
@@ -23,5 +26,17 @@ public void ParsePercentageText()
2326
Assert.AreEqual(99999, percentage.IntValue);
2427
});
2528
}
29+
30+
[ContractTestCase]
31+
public void TestCalculate()
32+
{
33+
"非零百分比除以自己等于 100%".Test((double value) =>
34+
{
35+
var percentage = Percentage.FromDouble(value);
36+
37+
var result = percentage / percentage;
38+
Assert.AreEqual(true, Math.Abs(result.DoubleValue - 1) < 0.000001);
39+
}).WithArguments(1.5, 2.3, 3.6, 100.5, 100000.123);
40+
}
2641
}
2742
}

0 commit comments

Comments
 (0)