Guest User

Untitled

a guest
Dec 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. std::cout << std::fixed << std::setprecision(std::numeric_limits<double>::digits10 + 1) << value << std::endl;
  2.  
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <limits>
  6.  
  7. void print(double value)
  8. {
  9. const auto digits = std::numeric_limits<double>::digits10;
  10. std::cout << std::setfill(' ') << std::setw(digits + 4);
  11. std::cout << std::fixed << std::setprecision(digits) << value << std::endl;
  12. }
  13.  
  14. int main()
  15. {
  16. double value = 9.12;
  17. print(value);
  18. }
  19.  
  20. class IM {
  21. double d;
  22. size_t ww;
  23. char c;
  24. public:
  25. IM(double val, size_t whole_size, char imbue = ' ')
  26. : d(val), ww(whole_size), c(imbue) {}
  27. int get_whole() const
  28. {
  29. return d;
  30. }
  31. int get_fraction() const
  32. {
  33. stringstream s;
  34. s << d - get_whole();
  35. int k;
  36. s.ignore(2); // пропускаем '0' и '.'
  37. s >> k;
  38. return k;
  39. }
  40. friend ostream& operator <<(ostream& os, const IM& m)
  41. {
  42. os << setw(m.ww) << setiosflags(ios_base::left) <<setfill(m.c)
  43. << m.get_whole() << '.' << m.get_fraction();
  44. return os;
  45. }
  46. };
  47.  
  48. double d = 2.45;
  49. IM dd(d, 10, '5');
  50. cout << dd << endl << dd.get_whole() << endl << dd.get_fraction();
  51. /* вывод:
  52. 2555555555.45
  53. 2
  54. 45
  55. */
Add Comment
Please, Sign In to add comment