Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Basics (Retired) Perfect Doubles

Derek Derek
Derek Derek
8,744 Points

using hour and minutes

I tried specifying the total time by converting it into hours and minutes. When I compile and run and enter, say 20 minutes, as the input, I get a weird conversion like 0.333333 hours and 20 minutes.. This all started when I switched to using doubles from using ints. Why is this happening and how can I fix it?

Here is the snapshot of my code:

https://w.trhou.se/o8ik1qs1ex

Thank you!

2 Answers

Steven Parker
Steven Parker
229,771 Points

When you switched from integer to double variables, the compiler also began using double math for you. Your program is intended to separate the hours from the minutes, and back when the values were integers, dividing total minutes by 60 gave you only the hours. But now, the double division gives you a number which includes the minutes expressed as fractions of an hour.

So now, to extract only the hours, you need to employ the math library function Floor which separates the whole portion from the fractions, as shown in this excerpt of your program:

            // Display total minutes excercised to the screen
            hour = Math.Floor(runningTotal / 60);  // <-- note change here
            minutes = runningTotal % 60;

To do it without using Floor, you could subtract the minutes part before you divide, like this:

            // Display total minutes excercised to the screen
            minutes = runningTotal % 60;           // <-- calculate minutes first
            hour = (runningTotal - minutes) / 60;  // <-- subtract them before dividing

Hi Hyun!

What exactly do you want to change because you are being given the right output? Did you want to cap the amount of trailing digits that the number can contain or something?

-Luke

Derek Derek
Derek Derek
8,744 Points

Before I changed my variables to doubles, when my input was 20 minutes, the code returned 0 hours and 20 minutes. Now, it returns 0.33333 hours and 20 minutes.. I was wondering if there is a way for me to keep the variables as doubles but make the code to return 0 hours and 20 minutes.. Thank you!

Hi!

If you wanted to you would have to write a more complex function to get it to work as you wish.

-Luke