Rounding Decimals

I have a problem dealing with answer in money, and after prompting the user for a number and performing a calculation using multiple functions, I am getting numbers with lots of decimals. Is there a function where you can specify wanting only 2 decimals points? I know that ceil and floor are functions for rounding, but they do not give me what I want. Any suggestions would be great!

http://www.cplusplus.com/reference/iomanip/setprecision/?kw=setprecision
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <iomanip>

int main()
{
	float num = 3.14159265359;
	std::cout << num << std::endl;
	std::cout << std::setprecision(9) << num << std::endl;
	std::cout << std::setprecision(7) << num << std::endl;
	std::cout << std::setprecision(4) << num << std::endl;
        std::cout << std::setprecision(2) << num << std::endl;
	std::cout << std::fixed;
	std::cout << std::setprecision(2) << num << std::endl;
	
	return 0;
}
3.14159
3.14159274
3.141593
3.142
3.1
3.14
Last edited on
I know that ceil and floor are functions for rounding
There are other functions too such as round which will round up or down instead of one or the other.

http://www.cplusplus.com/reference/cmath/
Rounding and remainder functions
ceil
Round up value (function )
floor
Round down value (function )
fmod
Compute remainder of division (function )
trunc
Truncate value (function )
round
Round to nearest (function )
lround
Round to nearest and cast to long integer (function )
llround
Round to nearest and cast to long long integer (function )
rint
Round to integral value (function )
lrint
Round and cast to long integer (function )
llrint
Round and cast to long long integer (function )
nearbyint
Round to nearby integral value (function )
remainder
Compute remainder (IEC 60559) (function )
remquo
Compute remainder and quotient (function )
Last edited on
Topic archived. No new replies allowed.