garryhtreez

6.14

Dec 8th, 2020
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. /*
  2. 6.14 Rounding Numbers
  3.    Deitel & Deitel C++ How to Program, 10th ed (Indian subcontinent adaptation)
  4.    Visual Studio Community 2019
  5. */
  6. #include <iostream>
  7. #include <cmath>
  8. #include <iomanip>
  9. using namespace std;
  10.  
  11. int roundToInteger(double);
  12. double roundToTenths(double);
  13. double roundToHundredths(double);
  14. double roundToThousandths(double);
  15.  
  16. int main()
  17. {
  18.    double num;
  19.    cout << "Enter number ( -1 to exit ): ";
  20.    cin >> num;
  21.  
  22.    while (num != -1)
  23.    {
  24.       cout << setprecision(12);
  25.       cout << num << " rounded to nearest integer: " << roundToInteger(num) << endl;
  26.       cout << num << " rounded to nearest 10th: " << roundToTenths(num) << endl;
  27.       cout << num << " rounded to nearest 100th: " << roundToHundredths(num) << endl;
  28.       cout << num << " rounded to nearest 1000th: " << roundToThousandths(num) << endl;
  29.       cout << endl;
  30.       cout << "Enter number ( -1 to exit ): ";
  31.       cin >> num;
  32.    }
  33.    return 0;
  34. }
  35.  
  36. int roundToInteger(double num) {
  37.    return floor(num + 0.5);
  38. }
  39.  
  40. double roundToTenths(double num) {
  41.    return floor(num * 10 + 0.5) / 10;
  42. }
  43.  
  44. double roundToHundredths(double num) {
  45.    return floor(num * 100 + 0.5) / 100;
  46. }
  47.  
  48. double roundToThousandths(double num) {
  49.    return floor(num * 1000 + 0.5) / 1000;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment