If you just want to round the number for output purposes, then the "%.2f" format string is indeed the correct answer. However, if you actually want to round the floating point value for further computation, something like the following works:
#include <math.h>
float val = 37.777779;
float rounded_down = floorf(val * 100) / 100; /* Result: 37.77 */
float nearest = roundf(val * 100) / 100; /* Result: 37.78 */
float rounded_up = ceilf(val * 100) / 100; /* Result: 37.78 */
Notice that there are three different rounding rules you might want to choose: round down (ie, truncate after two decimal places), rounded to nearest, and round up. Usually, you want round to nearest.
As several others have pointed out, due to the quirks of floating point representation, these rounded values may not be exactly the "obvious" decimal values, but they will be very very close.
For much (much!) more information on rounding, and especially on tie-breaking rules for rounding to nearest, see the Wikipedia article on Rounding.
Answer from Dale Hagglund on Stack OverflowIf you just want to round the number for output purposes, then the "%.2f" format string is indeed the correct answer. However, if you actually want to round the floating point value for further computation, something like the following works:
#include <math.h>
float val = 37.777779;
float rounded_down = floorf(val * 100) / 100; /* Result: 37.77 */
float nearest = roundf(val * 100) / 100; /* Result: 37.78 */
float rounded_up = ceilf(val * 100) / 100; /* Result: 37.78 */
Notice that there are three different rounding rules you might want to choose: round down (ie, truncate after two decimal places), rounded to nearest, and round up. Usually, you want round to nearest.
As several others have pointed out, due to the quirks of floating point representation, these rounded values may not be exactly the "obvious" decimal values, but they will be very very close.
For much (much!) more information on rounding, and especially on tie-breaking rules for rounding to nearest, see the Wikipedia article on Rounding.
Using %.2f in printf. It only print 2 decimal points.
Example:
printf("%.2f", 37.777779);
Output:
37.77
Videos
I want to round a double to one decimal point. I tried using round() but it doesnโt work the way I need it. I want to round it so 45.892 is 45.9. How would I do this?
To round a double up to 2 decimal places, you can use:
#include <iostream>
#include <cmath>
int main() {
double value = 0.123;
value = std::ceil(value * 100.0) / 100.0;
std::cout << value << std::endl; // prints 0.13
return 0;
}
To round up to n decimal places, you can use:
double round_up(double value, int decimal_places) {
const double multiplier = std::pow(10.0, decimal_places);
return std::ceil(value * multiplier) / multiplier;
}
This method won't be particularly fast, if performance becomes an issue you may need another solution.
If it is just a matter of writing to screen then to round the number use
std::cout.precision(3);
std::cout << gpa << std::endl;
see
floating points are not exactly represented so by internally rounding the value and then using that in your calculations you are increasing the inexactness.
You can use the following algorithm:
- multiply with 100
- round to nearest integer
- divide by 100
This algorithm has problems with big values that are very close to maximum representable as the multiplication may overflow.
Another approach is to use the stream manipulators as you would with std::cout but stream into a string, and then convert that string back into a floating point number. This is much slower, but has no caveat mentioned above.
You can't. Floating-point variables don't have decimal places: they have binary places, which are incommensurable with decimal places.
If you want decimal places, you have to use a decimal radix, e.g. when formatting for output.
I have a question when it comes to rounding in C. Does it round up or down at .5? If it does round up, then does that mean that the smallest value of k in the code below can only be 1?
int main()
{
int k = 13;
int i;
for (i = 0; i < 8; i++) {
printf("%d", (k%2));
k >>= 1;
}
printf("%n");
}Using recursion (which is going to be slow for some values of digits)
#include <math.h>
double my_round(double x, unsigned int digits) {
if (digits > 0) {
return my_round(x*10.0, digits-1)/10.0;
}
else {
return round(x);
}
}
A method likely to be somewhat faster, but which relies on a single call to the slow pow function:
#include <math.h>
double my_round(double x, unsigned int digits) {
double fac = pow(10, digits);
return round(x*fac)/fac;
}
An even faster method is to precompute a lookup table with the likely powers and use that instead of pow.
#include <math.h>
double fac[]; // population of this is left as an exercise for the reader
double my_round(double x, unsigned int digits) {
return round(x*fac[digits])/fac[digits];
}
Whilst "answerd" gives a decent answer, here's one that works for arbitrarily large numbers:
double round1(double num, int N) {
ASSERT(N > 0);
double p10 = pow(10,N);
return round(num* p10) / p10;
}
Of course, as stated, floating point numbers don't have a set number of decimal digits, and this is NOT guaranteed to PRINT as 3.70000 if you call printf("%8.5f", round1(3.7519, 1)); for example.