Not using Math.Truncate properly. Trying to show 26 instead of 26.8 in C#

Viewed 22

I'm creating a console application that checks if guy has beaten the record for climbing a mountain. There is a catch that says the guy loses 30 seconds every 50 meters i named this varible "wastedTime" and if its 26.5 seconds i want it to be calculated as 26 instead of 27 this is my code:

class Program
{
    static void Main(string[] args)
    {
        double record = double.Parse(Console.ReadLine());
        double range = double.Parse(Console.ReadLine());
        double timeForOneMeter = double.Parse(Console.ReadLine());

        double time = timeForOneMeter * range;

        double wastedTime = Math.Truncate((range / 50) * 30);
        double georgesTime = time + (wastedTime);

        Console.WriteLine(wastedTime);

        if (georgesTime<=record)
        {
            Console.WriteLine($"Yes! The new record is {georgesTime:f2} seconds.");
        }
        else if (georgesTime>record)
        {
            Console.WriteLine($"No! He was {georgesTime-record:f2} seconds slower.");
        }
    }
}
1 Answers

You can use Math.Floor method, documentation is here.

Returns the largest integral value less than or equal to the specified number.

For example:

Console.WriteLine(Math.Floor(26.99d));
// outputs: 26

C# Fiddle with example here.

Related