Thread: Convert type string to a number

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    26

    Convert type string to a number

    How would you go about converting a string contained in a TextBox to a numeric value?

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Code:
    <int variable> = int.Parse(<string value>);

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    The above solution should be absolutly sufficient for your program. However if you have different machines and cultures involved, use the following:

    Code:
    <int variable> = Convert.ToInt32(<string value>, CultureInfo.CurrentCulture );
    You can convert a lot of datatypes with Convert. Please make sure you pass a cultureinfo, otherwise it's machine specific and values send from one machine to another might not be convertable by the destination machine if it has different country/culture options. The example uses the current culture, and might break when you involve different cultures. For culture-independent conversion use CultureInfo.InvariantCulure.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM