How do I round up decimal places?

I'm taking a math oriented programming class and this assignment involves some payments. If a calculation for a payment comes out to something like this: 100.251, I want to round that value up to 100.26. Using fixed and setprecision(2) uses normal rounding rules.

I'm thinking it involves the 'ceil' function but it seems to be rounding up my values to the nearest whole integer. Any ideas?


Are you allowed to use <cmath>?
Multiply by 100 (10025.1), use ceil() (10026), then divide by 100 (100.26).
Ah-ha! That did the trick. Thanks Abstraction.
Using setprecision
Here you go:

http://www.cplusplus.com/forum/beginner/3600/.
**nevermind
Last edited on
closed account (E0p9LyTq)
You want to round up to a more significant decimal place, in your stated case hundreths. Simply multiply by 100, use ceil() to round up and then divide by 100.

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <cmath>

int main()
{
   std::cout << "100.251 rounded off to hundredths is " << (ceil((100.251 * 100)) / 100) << "\n";

   return 0;
}


100.251 rounded off to hundredths is 100.26
Last edited on
closed account (E0p9LyTq)
@supernoob, setprecision() only effects how the output is done. It doesn't actually change the number.

100.251 with setprecision(5) will still be stored as 100.251. The OP wants to change the actual value stored.
Topic archived. No new replies allowed.