Berryl
Berryl

Reputation: 12843

algorithm for working with ratios using decimal type

What technique would you use to make the test below pass?

Cheers,
Berryl

    [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

Upvotes: 0

Views: 179

Answers (1)

dtb
dtb

Reputation: 217341

Your test passes if you change Math.Floor to Math.Round.

Upvotes: 2

Related Questions