how to obtain a double from a string

I would like to convert the string without losing the decimal part and obtainig the same accuracy...

this is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>

using namespace std;

int _tmain(int argc, char* argv[]){

string param = "1.65"; 
double L3;

L3=atof(param.c_str());
 
}


If I use atoi L3 = 1.00000; and if I use atof L3 = 1.6499999999; Any idea??

Thank you!!
see this article it contains many different answers:
http://www.cplusplus.com/articles/D9j2Nwbp/
The types float and double do not store every possible floating-point number. In particular, 1.65 is a number that cannot cannot be stored in a variable of either type exactly.

The closest float is 1.64999997615814208984375, and the closest double is
1.649999999999999911182158029987476766109466552734375

What do you need it for? Why not just keep your string as-is?
Last edited on
Topic archived. No new replies allowed.