© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
26 replies
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;
    }
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;
    }
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Parse float/double and only allow digits and 0 or 1 dot
C#CC# / help
9mo ago
❔ My Level Number keep returning as 0 (Unity)
C#CC# / help
3y ago
adding a double and a float
C#CC# / help
15mo ago
✅ Adding a string to a double and then outputting it as a string
C#CC# / help
2y ago