C#C
C#2y ago
Cykotech

Truncating 1 and 0 and returning as a double

I'm having trouble with the Cars, Assemble exercise on the C# path on Exercism. I'm calculating the success rate based on the speed parameter and no matter how I round or truncate, whenever I try to return 0 or 1, my code only returns 0.9000000000002.

public static double SuccessRate(int speed)
    {
        double rate = Math.Round(0.0);
        
        if (speed >=1 || speed <= 4)
        {
            rate = Math.Round(1.0, 0);
        }
        if (speed >=5 || speed <=8)
        {
            rate = Math.Round(0.9, 1);
        }
        if (speed == 9)
        {
            rate = Math.Round(0.8, 1);
        }
        if (speed == 10)
        {
            rate = Math.Round(0.77, 2);
        }

        return rate;
    }
Was this page helpful?