Guest User

Untitled

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