0

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.

emadalamoudi
  • 357
  • 1
  • 2
  • 12
  • *"in C++"* Eh, no, this is not in C++ yet. Have you a reference for your particular implementation? – DeiDei Jul 08 '17 at 20:11
  • This is a TS for C++ – Justin Jul 08 '17 at 20:13
  • @DeiDei I'm sorry, I didn't understand your response. – emadalamoudi Jul 08 '17 at 20:14
  • my ultimate goal is to print the content of the std::decimal::decimal128 to the user. – emadalamoudi Jul 08 '17 at 20:16
  • 1
    "it gives an error." You need to read the error mesaage and understand what it says. Sorry can't do that for you. – n. m. could be an AI Jul 08 '17 at 20:29
  • This [answer](https://stackoverflow.com/a/12865735/8161208) suggests that decimal support in gcc is implemented in terms of libdecnum. – Volodymyr Lashko Jul 08 '17 at 21:41
  • `std::decimal` has not been added to any C++ standard yet. As such, your answer will depend on the exact implementation and what paper it implements and how well it does. – Yakk - Adam Nevraumont Jul 08 '17 at 22:10
  • Something like Integer.toString() in java? – Pushan Gupta Jul 08 '17 at 23:55
  • @n.m. I have read the error message but from what I understand is that the "to_string" function can not be applied to decimal128 – emadalamoudi Jul 09 '17 at 05:18
  • @VolodymyrLashko Yes, I have seen that answer but I couldn't figure how to apply it on my case. – emadalamoudi Jul 09 '17 at 05:19
  • @Yakk So, do you suggest using Boost instead? – emadalamoudi Jul 09 '17 at 05:20
  • @VidorVistrom Yes. I'm looking for something like that but to convert from decimal128 to string. – emadalamoudi Jul 09 '17 at 05:21
  • You should post the exact text of the error message as a part of your question but anyway. It looks like your compiler doesn't support decimas fully. Remember they are not a part of the C++ standars yet. You may be able to write your own to_string implementation using stringstream. – n. m. could be an AI Jul 09 '17 at 06:02
  • @n.m. Thanks for replying! I have tried the following: int main (int argc, char *argv[]) { std::decimal::decimal128 total(1.3265465468484); float a = 5.23; std::stringstream ta; std::stringstream tb; ta << a; tb << total; ta.precision(2); tb.precision(2); std::string out = ""; out += ta.str() + "\n"; out += tb.str() + "\n"; return 0; } It works fine with float but not with decimal128. – emadalamoudi Jul 09 '17 at 06:40
  • I suggest you tell us the exact implementation you are using, and ideally which paper it is trying to implement. C++ is way too vague. Which compiler - not stated. What version - not stated. What flags used so that include works - not stated. "C++" does not describe your problem, because this is not part of C++ yet. – Yakk - Adam Nevraumont Jul 09 '17 at 10:53
  • @Yakk, sorry, I'm new so that's why I don't know much about c++. I'm using Netbeans IDE 8.2 on Windows 10. I'm also using MinGW with g++ compiler. The C++ standard is C++14. – emadalamoudi Jul 09 '17 at 14:11
  • What I want to do is to write the value of the Decimal128 variable on a text file. That's why I need to convert it to string. If I can write it without conversion that would solve the puzzle too. – emadalamoudi Jul 09 '17 at 14:13
  • any answer yet! :( – emadalamoudi Jul 12 '17 at 08:15

1 Answers1

0

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.

emadalamoudi
  • 357
  • 1
  • 2
  • 12
  • 1
    This loses precision awfully: `decimal128` contains 34 decimal digits, while common implementations of `double` are `binary64`, which can generally only round-trip 15 decimals. Even `long double` (even if done correctly, not as in the code here) would be insufficient: the native x87 80-bit format can generally round-trip 18 decimal digits only. – Ruslan Sep 29 '20 at 19:29