fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <sstream>
  4.  
  5. int main() {
  6. std::string data = "3.236568949";
  7. std::stringstream ss(data);
  8. long double ld;
  9. ss >> ld;
  10.  
  11. std::cout << "Size (in bytes) of a double: " << sizeof(double)
  12. << " while a long double: " << sizeof(long double) << '\n'
  13. << "Original string: " << data << '\n'
  14. << "Converted number:\n" << std::setprecision(10) << ld << '\n'
  15. << "Same number, but with more outputted decimal figures:\n"
  16. << std::setprecision(100) << ld << '\n';
  17.  
  18. std::cout << "\nNow let's try to limit the precision.\n";
  19.  
  20. std::stringstream ss2;
  21. long double ld2;
  22. ss2 << std::setprecision(4) << ld;
  23. ss2 >> ld2;
  24. std::cout << "Converted number:\n" << std::setprecision(10) << ld2 << '\n'
  25. << "Same number, but with more outputted decimal figures:\n"
  26. << std::setprecision(100) << ld2 << '\n';
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Size (in bytes) of a double: 8 while a long double: 16
Original string: 3.236568949
Converted number:
3.236568949
Same number, but with more outputted decimal figures:
3.23656894899999999994262556146651377275702543556690216064453125

Now let's try to limit the precision.
Converted number:
3.237
Same number, but with more outputted decimal figures:
3.23699999999999999994622357224471898007323034107685089111328125