Skip to content

Commit

Permalink
Merge pull request #43 from dotnet-campus/t/lindexi/Percentage
Browse files Browse the repository at this point in the history
修复百分比计算
  • Loading branch information
SeWZC authored Feb 7, 2024
2 parents 6602f56 + 4094670 commit 3a8087c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/dotnetCampus.OpenXmlUnitConverter/Percentage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ public override int GetHashCode()
/// <returns></returns>
public static Percentage operator *(Percentage a, Percentage b)
{
return new Percentage(a.IntValue * b.IntValue);
// 相乘 和 相除 都要考虑倍率,不如直接靠 DoubleValue 来计算
return FromDouble(a.DoubleValue * b.DoubleValue);
}

/// <summary>
Expand All @@ -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);
}

/// <summary>
Expand Down
17 changes: 16 additions & 1 deletion tests/dotnetCampus.OpenXmlUnitConverter.Tests/PercentageTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using MSTest.Extensions.Contracts;

namespace dotnetCampus.OpenXmlUnitConverter.Tests
Expand All @@ -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);
}
}
}

0 comments on commit 3a8087c

Please sign in to comment.