Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 6.14 Rounding Numbers
- Deitel & Deitel C++ How to Program, 10th ed (Indian subcontinent adaptation)
- Visual Studio Community 2019
- */
- #include <iostream>
- #include <cmath>
- #include <iomanip>
- using namespace std;
- int roundToInteger(double);
- double roundToTenths(double);
- double roundToHundredths(double);
- double roundToThousandths(double);
- int main()
- {
- double num;
- cout << "Enter number ( -1 to exit ): ";
- cin >> num;
- while (num != -1)
- {
- cout << setprecision(12);
- cout << num << " rounded to nearest integer: " << roundToInteger(num) << endl;
- cout << num << " rounded to nearest 10th: " << roundToTenths(num) << endl;
- cout << num << " rounded to nearest 100th: " << roundToHundredths(num) << endl;
- cout << num << " rounded to nearest 1000th: " << roundToThousandths(num) << endl;
- cout << endl;
- cout << "Enter number ( -1 to exit ): ";
- cin >> num;
- }
- return 0;
- }
- int roundToInteger(double num) {
- return floor(num + 0.5);
- }
- double roundToTenths(double num) {
- return floor(num * 10 + 0.5) / 10;
- }
- double roundToHundredths(double num) {
- return floor(num * 100 + 0.5) / 100;
- }
- double roundToThousandths(double num) {
- return floor(num * 1000 + 0.5) / 1000;
- }
Advertisement
Add Comment
Please, Sign In to add comment