How do I round up decimal places?

Aug 5, 2015 at 7:32pm
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?


Aug 5, 2015 at 7:34pm
Are you allowed to use <cmath>?
Aug 5, 2015 at 7:34pm
Aug 5, 2015 at 7:37pm
Multiply by 100 (10025.1), use ceil() (10026), then divide by 100 (100.26).
Aug 5, 2015 at 7:39pm
Ah-ha! That did the trick. Thanks Abstraction.
Aug 5, 2015 at 7:40pm
Using setprecision
Here you go:

http://www.cplusplus.com/forum/beginner/3600/.
**nevermind
Last edited on Aug 5, 2015 at 7:48pm
Aug 5, 2015 at 7:42pm
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 Aug 5, 2015 at 7:44pm
Aug 5, 2015 at 7:51pm
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.