I have double
i=0.9500000000000000000000000001;
How can i convert it to string?
Like this
"0.9500000000000000000000000001"
I have double
i=0.9500000000000000000000000001;
How can i convert it to string?
Like this
"0.9500000000000000000000000001"
It seems that the precision you request is not only beyond double
, but also beyond long double
(at least for my version of gcc). For long double
I would suggest something like this:
#include<iostream>
#include<iomanip>
#include<limits>
int main()
{
// let's use number 1/3 to test possible limits of long double
long double i = 1.0L/3;
// create a stream
std::ostringstream streamLong;
// set precision to the maximal limit
streamLong << std::fixed << std::setprecision(std::numeric_limits<long double>::digits10 + 1);
// read long double to stream
streamLong << i;
// convert streamLong to a string
std::string i_string = streamLong.str();
std::cout << "i_string=" << i_string << std::endl;
return 0;
}
You can find some other possible solutions here.