convert string to float

Discussion in 'C++' started by Juhan Voolaid, Feb 13, 2005.

  1. Hi

    I'm having hard time finding a way to convert:

    std::string input="10.5";
    to
    float input2;

    help is appreciated,
    Juhan Voolaid.
     
    Juhan Voolaid, Feb 13, 2005
    #1
    1. Advertising

  2. Juhan Voolaid

    Sumit Rajan Guest

    Sumit Rajan, Feb 13, 2005
    #2
    1. Advertising

  3. Juhan Voolaid

    Ron Natalie Guest

    Juhan Voolaid wrote:
    > Hi
    >
    > I'm having hard time finding a way to convert:
    >
    > std::string input="10.5";
    > to
    > float input2;


    There are two ways. C gives you strtod which converts between a char array
    and double:
    input2 = strtod(input.c_str(), NULL);

    The C++ streams provide nice conversions to and from a variety of types.
    The way to use strings with streams is to use a stringstream:

    istringstream in(input);
    input >> input2;
     
    Ron Natalie, Feb 13, 2005
    #3
  4. Juhan Voolaid

    pete142

    Joined:
    Feb 7, 2011
    Messages:
    1
    In C:

    char input[] = "10.5";
    double num;

    num = strtod( input, NULL ); // to double
    printf( "%8.3", num ); // back to string

    -- pete
     
    Last edited: Feb 7, 2011
    pete142, Feb 7, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Quinn

    Convert float to string

    Quinn, Aug 6, 2003, in forum: Java
    Replies:
    1
    Views:
    8,578
    Roedy Green
    Aug 6, 2003
  2. Andy
    Replies:
    7
    Views:
    6,331
    Roedy Green
    May 10, 2004
  3. bd
    Replies:
    0
    Views:
    668
  4. Carsten Fuchs
    Replies:
    45
    Views:
    1,655
    James Kanze
    Oct 8, 2009
  5. Daniel Firu
    Replies:
    2
    Views:
    287
    Jesús Gabriel y Galán
    Jan 17, 2009
Loading...

Share This Page