String to Double

3.459583967924D-05

I have this number above stored in a text file. I extract the value in a string and convert it to a double value and get the number below.


3.45958396792399

The number should actually read : 0.0000345958396792

The code below is what i used to convert it from string to double.

1
2
3
4
5
6
7
8
9
10
11
12
double GetDblVal(string strConvert) {// Converts string into double

  istringstream stm;
  cout<<setiosflags(ios::fixed | ios::showpoint | ios::right) << setprecision(18);
  double dblReturn =0.0000000;
  stm.str(strConvert);
  stm >> dblReturn;

  return(dblReturn);
}




Any suggestions on how i should handle the string with the number " 3.459583967924D-05 " stored in it?
Last edited on
change your D to an E?
3.459583967924D-05

3.459583967924E-05

it displays ok for me. never heard of D Notation.

maybe this?
 
strConvert.replace(strConvert.find("D"), 1, "E");

i didn't try the replace code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
double GetDblValPower(string strConvert) {// Converts string into double, shifting decimal places.
  double dblReturn=0.0, dblReturn_temp,base, exp;
  base=10;   exp=0;
  string pos_d;
  //cout<<setiosflags(ios::fixed | ios::showpoint ) << setprecision(19);
  istringstream stm;
  stm.str(strConvert);
  stm >> dblReturn_temp;

  if (strConvert.find("D"))
  {
   pos_d=strConvert.substr(16,3);
   exp=GetDblVal(pos_d);
   dblReturn=dblReturn_temp * pow(base,exp);

  }

  return(dblReturn);
}


This is what I came up with... I think it works as it should, haven't come across any bugs.

Thanks for the help.

oh em gee, you stole my name :P
Topic archived. No new replies allowed.