Thread: Convert.ToDouble question

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    80

    Convert.ToDouble question

    I've bought a C# book which teaches me to convert my input to a double like this:
    Code:
    double number;
    number = Convert.ToDouble(Console.ReadLine());
    When I enter something like 10.12 it gives me an error and accuses me for entering info of the wrong type.
    I use Visual C# 2005 Express. Any ideas?

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Code looks good.
    Code:
    static void Main(string[] args)
    {
    	double number;
    	Console.Write("Enter a double: ");
    	number = Convert.ToDouble(Console.ReadLine());
    	Console.WriteLine("You entered " + number.ToString());
    	Console.ReadLine();
    }
    Output:
    Code:
    Enter a double: 10.15
    You entered 10.15
    So, what exactly is the problem again?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    80
    I think I figured it out... my language setting is swedish, and in sweden we use a comma instead of a dot when we use floats like this: 10,15 instead of 10.15 and when I input that at runtime it works fine. So I guess the .NET framework adapts to different national standards... this is the only reason I can think of since it works with a comma.
    Last edited by antex; 05-16-2006 at 11:06 AM.

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Yup, sounds pretty logical to me. You can specify your culture settings in the Convert call to allow other representations.
    Code:
    static void Main(string[] args)
    {
    	double number;
    	NumberFormatInfo numberInfo = new NumberFormatInfo();
    	numberInfo.NumberDecimalSeparator = ",";
    	Console.Write("Enter a double: ");
    	number = Convert.ToDouble(Console.ReadLine(),numberInfo);
    	Console.WriteLine("You entered " + number.ToString(numberInfo));
    	Console.ReadLine();
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    80
    Ah! Thank you for this info. I think I'll change to the more international "." since that's what I'm used to in the world of programming. Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM