convert from string to double !

This is a discussion on convert from string to double ! within the C++ Programming forums, part of the General Programming Boards category; I got a string that contains numbers let's say "1.54251" I want to convert this string to double because I ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    17

    convert from string to double !

    I got a string that contains numbers let's say "1.54251"
    I want to convert this string to double because I want to do some operations on that number
    I tried normal casting but didn't work.
    I remember something about type_cast but I can't get it to work.

    Can anyone help me with this ?
    Thanks.

    EDIT: I tried this
    static_cast<double>(var);
    and I got this
    Error 1 error C2440: 'static_cast' : cannot convert from 'std::string' to 'double'
    Last edited by Shady; 10-12-2006 at 05:21 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    You don't type cast strings (or character arrays) containing numeric digits directly into ints or doubles. You need to convert them using other means... usually using something like stringstreams, sscanf, or one of the many functions available that do such things (strtod as an example).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    OK I searched MSDN library for the strtod and I found a program but I can't understand this part.
    string = "3.1415926This stopped it";
    x = strtod( string, &stopstring );
    How does it work ?
    and what if the text is at the beginning of the string ?

    It worked for me btw thanks a lot.
    Last edited by Shady; 10-12-2006 at 05:49 AM.

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    3.1415926 is what the double will contain.
    "This stopped it" just shows that when a non-numeric character comes, it just stops converting.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,470
    In &stopstring you are passing the address to a pointer to char that will store the address where the conversion has ended. In your example this will return the address of "T".
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String to Double function
    By jaro in forum C Programming
    Replies: 4
    Last Post: 05-27-2006, 11:10 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21