1. round

hi everyone

I would like to roungd 2.34567 to 2.34, can I have something like that in C++.

if you see this
Code:
```#include <stdio.h>
#include <math.h>

int main ()
{
printf ("floor of 2.3 is %.1lf\n", floor (2.3) );
printf ("floor of 3.8 is %.1lf\n", floor (3.8) );
printf ("floor of -2.3 is %.1lf\n", floor (-2.3) );
printf ("floor of -3.8 is %.1lf\n", floor (-3.8) );
return 0;
}```
the output is 2.0
3.0
-2.3
-3.8

I do want something like this, I would like to reduce the numbers after the point.

so this number 2.456633 would lke it to be 2.46

What can I use, what math operator.

2. Originally Posted by yes
hi everyone

I would like to roungd 2.34567 to 2.34, can I have something like that in C++.
sure you don't mean round up to 2.35?

Code:
```#include <iostream>

using namespace std;

int main ()
{
double f = 3.14159;
cout.precision(2);
cout<< f << endl;

return 0;
}```

3. If X is your variable, just do it like this:

X=round(X*10)/10;

Will round X to the first decimal.

I recommend you use std::cout instead of printf by the way. Streams also have an option to change the precision of their output of floating point numbers, which is much easier than rounding everything by hand. (in other words, do what twomers told you to)

4. Do you want to store the rounded number in a variable, or just output the rounded version and keep the variable as is?

 beaten...twice! damn slow connection... [/edit]

5. I would like just to round my Avg in my output file.

The Avg gets it value after a calculation and print its out put in a stream file. so now How do I round the Avg only.if my output statement like below;

Code:
`outfile << Avg << Min << Max << endl;`
thank you

6. If you want rounding for output only try this
Code:
`outfile << fixed << setprecision(2) << Avg << Min << Max << endl;`
you need to #include <iomanip>
Kurt

7. Thank you so much ZUK it is the perfect thing to my outfile.

8. Originally Posted by ZuK
If you want rounding for output only try this
Code:
`outfile << fixed << setprecision(2) << Avg << Min << Max << endl;`
you need to #include <iomanip>
Kurt
I was looking for this answer.. Thanks for very useful information.. I love this board..