long double to string & string to long double 
Author Message
 long double to string & string to long double

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.

I an doing mostly MFC stuff so if there is a class that I can use,
that would be great. Otherwise some sample code would be helpfull. I
tried to find something in the on-line help but I can not seem to find
much documented support for working with long doubles.

TIA
Dave



Sat, 06 Apr 2002 03:00:00 GMT  
 long double to string & string to long double
Check out the C function sprintf()


Sat, 06 Apr 2002 03:00:00 GMT  
 long double to string & string to long double

Quote:
>What is the best way to convert a long double to a string?

Dave,

Long doubles don't exist in 32-bit MS compilers - there's just float
and double:

"Previous 16-bit versions of Microsoft C/C++ and Microsoft Visual C++
supported the long double, 80-bit precision data type. In Win32
programming, however, the long double data type maps to the double,
64-bit precision data type.
"

Try _ecvt, _fcvt, _gcvt

Quote:
>I also need to be able to convert a string to a long double.

atof

Dave Lowndes
--
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.



Sun, 07 Apr 2002 03:00:00 GMT  
 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:
}



Sun, 07 Apr 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Difference between double & long double

2. Long long and long double

3. Long double to string --

4. Need function to convert float/double/long to string using a specified format

5. convert string to long double - problem

6. Double to string: _fcvt no longer works ???!!!

7. convert string to long, and long to string

8. can long double be less precise than double?

9. long long integer and double precision number

10. addition long values to long double value ?

11. epsilon for float, double long double

12. Difference between double and long double?

 

 
Powered by phpBB® Forum Software