您会使用什么技术来使下面的测试通过?
干杯,
贝瑞尔
[Test]
public void PercentageToSplit() {
for (int i = 1; i < 20; i++) {
decimal ratio = 1m / i;
Console.WriteLine(ratio);
var splitCount = (int)Math.Floor(1 / ratio); // *** this won't do
Console.WriteLine(splitCount );
Assert.That(splitCount, Is.EqualTo(i));
}
1 becomes 1
0.5 becomes 2
0.3333333333333333333333333333 becomes 3
0.25 becomes 4
0.2 becomes 5
0.1666666666666666666666666667 becomes 5
Test 'PercentageToSplit' failed:
Expected: 6
But was: 5
最佳答案
如果将 Math.Floor
更改为 Math.Round
,则测试通过。
关于c# - 使用小数类型处理比率的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8774668/