Results 1 to 4 of 4

Thread: Console.ReadLine() --> double variable

  1. #1

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Console.ReadLine() --> double variable

    How do I get the user's number into the double?

    Code:
    string temp;
    double myDBL;
    
    Console.Write("Enter a number: ");
    temp = Console.ReadLine(); //something like "231.44324"
    //perform some tests on the string to make sure its ok
    //...
    
    myDBL= temp; //causes compiler cast error
    Error: Cannot convert type 'string' to 'double'
    I don't live here any more.

  2. #2
    Hyperactive Member Sgt-Peppa's Avatar
    Join Date
    Mar 2003
    Location
    Munich - Germany
    Posts
    476
    Here you go:

    Code:
    myDBL= Double.Parse(temp);
    That should work.
    HTH,

    Stephan
    Keep Smiling - even if its hard
    Frankie Says Relax, wossname Says Yeah!
    wossname:--Currently I'm wearing a gimp suit and a parachute.
    C# - Base64 Blog

  3. #3

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    OK thanks, is that the normal way to do it?
    I don't live here any more.

  4. #4
    Lively Member
    Join Date
    Oct 2003
    Location
    Guildford, UK
    Posts
    91
    Don't bother with the checking part, just cast it and catch exceptions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width