The program behavoir is different on different systems. why ?

This is the code of my program:

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;
            while (!int.TryParse(Console.ReadLine(), out gold))
            {
                Console.WriteLine("Please enter a valid number for gold.");
                Console.WriteLine("Inesrt your gold: ");
            }
            Console.WriteLine("Inesrt your time(In Hours) played: ");
            float hours;
            while (!float.TryParse(Console.ReadLine(), out hours))                
                    {
                        Console.WriteLine("Please enter a valid number for hours.");
                        Console.WriteLine("Inesrt your hours played: ");
                    }
                    float time = ((int)hours) * 60 + (hours % 1) * 100; ; // 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.\n but no thanks");
                    Console.ReadLine();

                    //Console.WriteLine(" \nApp self destruct!");
                    //Console.ReadLine();

        }
    }
}

When I try to run it using my local Visual Studio environment, I see in my console that the output of minutes is equals to 900 when passing 1.5 hours into my program.

If I run this on www.ideone.com, I see that the output is 90 minutes for the same value 1.5.

Where could I make a mistake in my code ? Why the behaviour of my program is different when running in different places ?

Jon Skeet
people
quotationmark

I strongly suspect that when you run it locally, you're in a culture where , is the decimal separator rather than . - and perhaps . is the thousands separator, which is basically ignored. So 1.5 ends up being parsed as 15 hours, which is 900 minutes.

To validate this, try entering 1,5 instead - I suspect you'll then get a result of 90.

If you want to enforce settings where . is the decimal separator, simply pass a culture into float.TryParse:

while (!float.TryParse(Console.ReadLine(), NumberStyles.Float,
                       CultureInfo.InvariantCulture, out hours))

Note that you don't need to do all the arithmetic yourself - use TimeSpan to do it for you.

int minutes = (int) TimeSpan.FromHours(hours).TotalMinutes;

people

See more on this question at Stackoverflow