Is there any mechnisam for roundoff float numbers in c++
Hello guys..i am working on a program where in calculation if i got for example 14.6 i have to round off it to 15..how to do this??
In a simple case, something like this would suffice:
1 2
|
double f = 14.6 ;
int n = f + 0.5 ;
|
try:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float f = 27.34576;
cout<<"\n\n";
cout<<"f: = "<<f;
cout<<"\nf rounded up: = "<<ceil(f);
cout<<"\nf with setprecision(5): = "<<setprecision(5)<<f;
cout<<"\n";
return 0;
}
/*f: = 27.3458
f rounded up: = 28
f with setprecision(5): = 27.346 note the 6 here*/
|
Last edited on
Topic archived. No new replies allowed.