0

I've got stuck in my program, i need to calculate the gold/minute but my math formula won't do the desire thing. As I input the hours into a float(something like 1.2 hours) the transformation will be 72 min instead of 80 as I need. Can you please help me ? I marked in comment below where the problem is. And here is my code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YourGold
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to YourGold App! \n------------------------");
            Console.WriteLine("Inesrt your gold: ");
            int gold = int.Parse(Console.ReadLine());
            Console.WriteLine("Your gold is : " + gold);
            Console.WriteLine("Inesrt your time(In Hours) played: ");
            float hours = float.Parse(Console.ReadLine());
            int minutes = 60;
            float time = (float)hours * minutes; // Here the calculation are wrong...
            Console.WriteLine("Your total time playd is : " + time + " minutes");
            float goldMin = gold / time;
            Console.WriteLine("Your gold per minute is : " + goldMin);
            Console.WriteLine("The application has ended, press any key to end this app. \nThank you for using it.");
            Console.ReadLine();

        }
    }
}

Thanks a lot.

P.S it's related to this question:Allow only numbers to be inserted & transform hours to minutes to calculate gold/min - UPDATED , I update it same as this but i think i should have done a new question as i did now(I'm still learning how to go on with this platform:) )

Community
  • 1
  • 1
Victor
  • 468
  • 1
  • 5
  • 17
  • 8
    1.2 hours **is** 72 minutes – Dariusz Sep 19 '13 at 08:31
  • `.2` == `1/5` == `12/60`, why are you expecting 80? – Sayse Sep 19 '13 at 08:32
  • 1.2 hours _are_ 72 minutes. From where on earth do you get your expected result of 80 minutes? How can we possibly know what you want if we don't know the reasoning behing that? – Daniel Daranas Sep 19 '13 at 08:32
  • 1
    The OP's expectation is that 1.20 hours = 1 hours + 20 minutes. – Jon Sep 19 '13 at 08:32
  • 1
    @Jon then the OP's expectation is wrong. – Daniel Daranas Sep 19 '13 at 08:33
  • 60*1.2 = 72. Am I missing something? – Carra Sep 19 '13 at 08:33
  • 4
    @Jon the OP uses wrong syntax, it should be 1:20, not 1.2 – Dariusz Sep 19 '13 at 08:33
  • You need 80 as your output? But that is not valid. Dariusz is right it should be 1:20. – Anirudh Agarwal Sep 19 '13 at 08:34
  • 1
    You should not have reposted. Also what is wrong with the answers on the previous one? – H H Sep 19 '13 at 08:35
  • 1
    @DanielDaranas: You were wondering about the 80 minutes result so I provided an explanation. The responses should better be directed to the OP themselves, not to the messenger. – Jon Sep 19 '13 at 08:37
  • @Henk Holterman It is a duplicate yes, my apology. I needed an answer and didn't know if i should have post a new qusetion or update that one. Sorry for that and Carra and other asking that 1.2 is 72 i didn't know how to transform that 1.2 into 1:20 as Jon said. Anyway thanks all for the help here!!!! – Victor Sep 19 '13 at 08:37
  • @Jon You are right. I didn't mean to argue against your point. It is helpful, actually. The OP gets notified about comments to the question anyway so I mentioned you to make my comment more in context. – Daniel Daranas Sep 19 '13 at 08:38
  • @HenkHolterman - That question seems to have been edited to change what question was being asked – Sayse Sep 19 '13 at 08:38
  • And why do you think you will get better/different answers this time? – H H Sep 19 '13 at 08:39
  • And maybe as i see i didn't made my self clear, i don't need 72 to be posted as it is now(that's correct in terms of what code i provided) but i need that 1.2 to be transformed in 1:20 as Jon said. So if i didn't made my self clear,sorry. – Victor Sep 19 '13 at 08:47
  • Your own text doesn't even say that "Insert time (in hours)", 1.2 is a perfectly valid time in hours, such as saying 1.5 hours is 1 and a half hours. You'd be best to just ask the user to enter hours first then minutes after then there is no confusion anywhere – Sayse Sep 19 '13 at 08:49
  • Yup it is. I modified the hole thing and that's done. With all of your answers i've manage to do it more then 1 way and understand what wrongs i've done. Question answered and all you guys are very helpful. Virtual beer for everyone :) – Victor Sep 19 '13 at 08:53
  • @ЗапорожанВиктор - We don't work off virtual beers here, accept an answer. – Sayse Sep 19 '13 at 08:54
  • @Sayse i did that and the virutal beer was a funny thank you for your help... u didn't have to be like that :). anyway thnx again. – Victor Sep 19 '13 at 08:57
  • You've asked 10 questions it seems and only accepted 3 answers, just sayin'. – Sayse Sep 19 '13 at 09:02
  • I've asked the same question always, just read the question. But it's ok :) – Victor Sep 19 '13 at 09:19

4 Answers4

3

Use the built-in TimeSpan:

TimeSpan time = TimeSpan.FromHours(1.2);
double minutes = time.TotalMinutes;

TimeSpan.FromHours Method Returns a TimeSpan that represents a specified number of hours, where the specification is accurate to the nearest millisecond.

You can also do:

// string timeAsString = "1:20";
TimeSpan time;
if (TimeSpan.TryParse(timeAsString, CultureInfo.InvariantCulture, out time))
{
    double minutes = time.TotalMinutes;
    //... continue 
}
else
{
    // Ask user to input time in correct format
}

Or:

var time = new TimeSpan(0, 1, 20, 0);
double minutes = time.TotalMinutes;
Johan Larsson
  • 17,112
  • 9
  • 74
  • 88
2

If you really want your program to behave as you want do this.

time = (int)hours * 60 + (hours%1)*100
coder11
  • 457
  • 3
  • 9
1
var minutes = TimeSpan.FromHours(1.2).TotalMinutes; // returns 72.0
Alireza
  • 10,237
  • 6
  • 43
  • 59
0
var hours = 1.2;
var minutes = ((int)hours) * 60 + (hours%1)*100;

And a side note: such way of inputting time is IMO not a good one. It'll be confusing and I guess that more often than not people will be actually entering 1:20 instead of 1.2, which'll break your application. And if not, they might be entering 1.5 thinking of 90 minutes. I know I would have done it like that.

Dariusz
  • 21,561
  • 9
  • 74
  • 114
  • 4
    Have you seen @coder11 answer? – Sergey Berezovskiy Sep 19 '13 at 08:45
  • @lazyberezovsky I have not, at least when I started writing. There was one more answer which had a multiplier of 10 for minutes, instead of 100. – Dariusz Sep 19 '13 at 08:48
  • So this is the answer that i kind a needed, i wanted that 1.2 to be transformed into 1:20 when i calculate the time. The code i provided was doing everything good but not what i needed. Thanks anyway all of you for providing answers for me in more manners and i have seen my mistakes. Thanks agian! – Victor Sep 19 '13 at 08:50
  • Dariusz is right, I corrected it after I ran it only to see output 62! – coder11 Sep 19 '13 at 08:51