Guest User

Untitled

a guest
Dec 19th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 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. #include <iostream>
  21. #include <iomanip>
  22. #include <sstream>
  23. using namespace std;
  24.  
  25. class IM {
  26. double d;
  27. size_t ww;
  28. char c;
  29. public:
  30. IM(double val, size_t whole_size, char imbue = ' ')
  31. : d(val), ww(whole_size), c(imbue) {}
  32. int get_whole() const
  33. {
  34. return d;
  35. }
  36. int get_fraction() const
  37. {
  38. stringstream s;
  39. s << d - get_whole();
  40. int k;
  41. s.ignore(2); // пропускаем '0' и '.'
  42. s >> k;
  43. return k;
  44. }
  45. friend ostream& operator <<(ostream& os, const IM& m)
  46. {
  47. os << setw(m.ww) << setiosflags(ios_base::left) <<setfill(m.c)
  48. << m.get_whole() << '.' << m.get_fraction();
  49. return os;
  50. }
  51. };
  52.  
  53. double d = 2.45;
  54. IM dd(d, 10, '5');
  55. cout << dd << endl << dd.get_whole() << endl << dd.get_fraction();
  56. /* вывод:
  57. 2555555555.45
  58. 2
  59. 45
  60. */
Add Comment
Please, Sign In to add comment