long double to string & string to long double
Quote:
>What is the best way to convert a long double to a string?
>I also need to be able to convert a string to a long double.
Here is what I came up with. If anyone can see potential problems with
this approach, please let me know.
long double CHwk03Dlg::CStr2LDbl(CString cstr)
{ char* str;
long double ldbl;
//Get the string from CString object
str=cstr.GetBuffer(0);
//Make input stream point to string
istrstream InStr(str,(cstr.GetLength()+1));
InStr.clear(); //Clear input good bit
InStr.seekg(0,ios::beg); //Point to first char
InStr >> ldbl; //Get float
return ldbl; //Return float
Quote:
}
CString CHwk03Dlg::LDbl2CStr(long double ldbl)
{ char str[80]={"\0"};
CString cstr;
//Point output stream to string
ostrstream OutStr(str,80);
OutStr << ldbl; //Output ldbl to string
// I tried sprint but ostrstream works better
// sprintf(str, "%lf", ldbl);
cstr=str; //Assign string to CString
return cstr; //Return CString
Quote:
}