Rounding numbers

How would you round a positive number number to the nearest hundred?
Here is what I came up with:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main(){
	int cinValue;
	cout << "Please enter a value: " << endl;
	cin >> cinValue;
	int roundOff = cinValue % 100;
	int finalVar = cinValue - roundOff;
	cout << "Rounded to the nearest hundredth: " << finalVar << endl;
	
	return 0;
}
Thank You
Problem with orangepeel367's solution is that it always rounds down.

Please enter a value: 
199
Rounded to the nearest hundredth: 100


To fix this, simply add a check. If roundOff is greater than or equal to 50, round up and vice-versa with less than 50.
this is kinda confusing. is their a way that i can use the ceil() function to round the number.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{
	int cinValue;
	cout << "Please enter a value: " << endl;
	cin >> cinValue;

	int finalVar = ((cinValue % 100) < 50) ? cinValue - (cinValue % 100) : cinValue + (100 - (cinValue % 100));

	cout << "Rounded to the nearest hundredth: " << finalVar << endl;
	
	return 0;
}
I figured out the problem i was having and it is because the number i am using to round is a double.
Why are you using a double? Everything we can see here is int.
Becuase the number can have decimals and i use the same value to also find a percent and i didnt realize that % only works for int.
So did you solve your problem?
If you are having trouble with rounding a double, post your code here so that we can help.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
using namespace std;

int main() {

	
	bool game = true;
	do {
		
		char choice[25];
		double budget_given, amount_spent, percent,rounding,final_round;
		double new_budget, round2,round;
		char dollar;

		cout << " Would you like to use the budget calculator (y/n)?" << endl;

		cin >> choice;

		if (choice[0] == 'y') {

			cout << "Enter the budget that you were allowed last year." << endl;

			
			cin.ignore();
			if (cin.peek() == '$') {
				cin >> dollar >> budget_given;
			}
			else {

				cin >> budget_given;
			}

			cin.ignore();
			cout << " How much money did you spend last year?" << endl;

			

			if (cin.peek() == '$') {
				cin >> dollar >> amount_spent;
			}
			else {
				cin >> amount_spent;
			}

			percent = (amount_spent / budget_given) * 100;

			round = amount_spent % 100;

			round2= amount_spent -round;
			
			new_budget = amount_spent + 100;

			cout << " You used " << percent << " % " << "of the previous budget" << endl;

			cout << "Your next budget will be " << new_budget <<"$"<< endl;
		}

		else if (choice[0] == 'n') {

			cout << " Have a nice day" << endl;
			game = false;
		}

	} while (game != false);
	system("pause");
	return 0;
}  
I want to round amount_spent to the nearest hundred.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
using namespace std;

double roundNearestHundredth(double d)
{
	int d_i = d;
	return ((d_i % 100) < 50) ? d_i - (d_i % 100) : d_i + (100 - (d_i % 100));
}

int main() 
{
	bool game = true;
	do 
	{	
		char choice[25];
		double budget_given, amount_spent, percent;
		double new_budget;
		char dollar;

		cout << " Would you like to use the budget calculator (y/n) : ";

		cin >> choice;

		if (choice[0] == 'y') 
		{
			cout << "Enter the budget that you were allowed last year : ";

			cin.ignore();

			if (cin.peek() == '$') 
			{
				cin >> dollar >> budget_given;
			}
			else 
			{
				cin >> budget_given;
			}

			cin.ignore();
			cout << " How much money did you spend last year? : ";

			if (cin.peek() == '$') 
			{
				cin >> dollar >> amount_spent;
			}
			else 
			{
				cin >> amount_spent;
			}

			percent = (amount_spent / budget_given) * 100;

			new_budget = roundNearestHundredth(amount_spent);

			cout << " You used " << percent << " % " << "of the previous budget" << endl;

			cout << "Your next budget will be " << new_budget <<"$"<< endl;
		}

		else if (choice[0] == 'n') 
		{
			cout << " Have a nice day" << endl;
			game = false;
		}

	} 
	while (game != false);

	system("pause");
	return 0;
}  
Topic archived. No new replies allowed.