emadalamoudi emadalamoudi - 3 years ago 156
C++ Question

How to convert a std::decimal::decimal128 to string in C++

I want to convert a

std::decimal::decimal128
into a
string
in C++. I have tried this:

int main(int argc, char** argv) {
std::decimal::decimal128 check1(0.1654689875468);
string ee;
ee=std::to_string(check1);

return 0;
}


but it gives an error. I have tried so many methods, yet no one works. Anyone have an idea? Thanks in advance.

Answer Source

I have solved the problem by creating the following function:

 std::string ToString(std::decimal::decimal128 dec)
{
    long double d(std::decimal::decimal128_to_double(dec));
    std::ostringstream oss;
    oss << std::scientific << std::setprecision(37) << d;
    return oss.str();
}

Later I can use it whenever I need it:

string name= ToString(std::decimal::decimal128_to_double(1.3246546787));

Thanks all for helping.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download