How to round a double

Hello,
I have this program where I have to do some calculations,which is no problem but the results should be one decimal place. For Example, if I have 9.09090901 I want it to be printed as 9.1. By any chance is there anyone can tell me how to do this or give some advice.Any help will be greatly appreciated.

Thanks In advance!!
Helios,
Thanks for the info, but it doesn't cover how to round a double one decimal place. It talks about rouding a double to an int. By any chance do you know how to round a double to double but one decimal place.
http://www.cplusplus.com/reference/iostream/manipulators/setprecision.html

You can alter the value of the double with this function:
1
2
3
4
5
6
7
8
9
10
11
double cint(double x){
	if (modf(x,0)>=.5)
		return x>=0?ceil(x):floor(x);
	else
		return x<0?ceil(x):floor(x);
}

double round(double r,unsigned places){
	double off=pow(10,places);
	return cint(r*off)/off;
}

Note: The above has been blind coded. You better test it first.
Last edited on
Also you can use:

For double values:
1
2
3
4
%.1lf 
%.2lf 
%.3lf 
....


For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>

int main(void)
{
    double num;
    
    printf("Enter double: ");
    scanf("%lf", &num);
    printf("%.2lf", num);
    
    getch();
    return 0;
}

 


This program waiting user to enter a double value, then print "clean" value, example, if user is enter a number 35.23526, program will display 35.23.

Test program and will see what happen.


Excuse for my bad English, but he is not my native language.

Last edited on
You could also use a function such as this:-

1
2
3
4
5
6
7
float rndup(float n)//round up a float type and show one decimal place
{
      n*=10;//where n is the multi-decimal float
      ceil(n);
      n/=10;
      return n;
}          

You would need to include the header file <cmath> which defines ceil()
Easiest way

#include <iomanip>

cout << fixed << setprecision(1) << 9.09090901 << endl;

and it's done
I am a new member and this is my first post. Please excuse me iIf I am repeating anything posted here or elsewhere. While the original post in this thread did say that the rounded results are to be printed rounded to one decimal place there is a disctintion if one simply wants the rounded data to be printed or carried forward to other calculations. In one case the printf format arguments will work well while in the other case a function like that posted by helios and buffbill or discussed in article 3638 may be required. One caution I would like to add is how thr round-up or round-down rule is applied to results like X.5. Either rule will build in an error with large amount of data if the rounded data is used. If this is a potential problem perhaps a counter and a modulus operation the counter to alternate between round-up and the round-down rules could be considered. Regards.
@buffbill
i m sorry but thr is a slight flaw in the code u gave.
for a number say 7.13, it'll return 7.2 instead of 7.1.so i propose a correction in ur code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
float rndup(float n)//round up a float type and show one decimal place
{
      float t;
      t=n-floor(n);
      if (t>=0.5)    
      {
              n*=10;//where n is the multi-decimal float
              ceil(n);
              n/=10;
              }
      else 
      {
              n*=10;//where n is the multi-decimal float
              floor(n);
              n/=10;
              }
      return n;
}          

floor() and ceil() both are defined in <cmath>
Topic archived. No new replies allowed.