Converting string with negative values to double/ float

Hi,

Anyone know how to convert a string with probably negative values into double/ float?

I have search the web but answer found were too complicated.

I have a string value, str = "-100" and i want to convert it into a double/ float for doing calculation.

What is the simplest way?
teocl5 wrote:
What is the simplest way?

I'd say, something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
    string str1 = "150.75";
    string str2 = "-100.5";

    double d1, d2;

    stringstream(str1) >> d1;
    stringstream(str2) >> d2;

    cout << d1 + d2 << endl;

    return 0;
}

Useful link -> http://cplusplus.com/reference/iostream/stringstream/
Topic archived. No new replies allowed.