Round() For Float in C++

Is there a function to round a float in C or do I need to write my own?

As Rob mentioned, you probably just want to print the float to 1 decimal place. In this case, you can do something like the following:

#include <stdio.h>
#include <stdlib.h>

int main()
{
float conver = 45.592346543;
printf("conver is %0.1f\n",conver);
return 0;
}

If you want to actually round the stored value, that's a little more complicated. For one, your one-decimal-place representation will rarely have an exact analog in floating-point. If you just want to get as close as possible, something like this might do the trick:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
float conver = 45.592346543;
printf("conver is %0.1f\n",conver);

conver = conver*10.0f;
conver = (conver > (floor(conver)+0.5f)) ? ceil(conver) : floor(conver);
conver = conver/10.0f;

//If you're using C99 or better, rather than ANSI C/C89/C90, the following will also work.
//conver = roundf(conver*10.0f)/10.0f;

printf("conver is now %f\n",conver);
return 0;
}

I doubt this second example is what you're looking for, but I included it for completeness. If you do require representing your numbers in this way internally, and not just on output, consider using a fixed-point representation instead.

Round float in C in STM32F

Your code is probably correctly rounding 8.07999992 to 8.08. But then it's probably getting printed out as 8.07999992 again. This is due to the limited precision of floating point (and the even more limited precision of type float as opposed to double), and the fact that, in binary, there is no such number as 8.08.

Try these two things:

  1. Print your result using %.2f, and again using %.20f. (I assume you're using printf.)
  2. Use type double instead of float.

The other question to ask is, why are you trying to round these numbers? Is it because they're only significant to two places? Is it because you want to display them that way? Is it because they're supposed to represent dollars and cents (or some other centidecimal currency)?

  • If the quantities are only significant to two places, rounding as you're currently doing should be correct.
  • If you want to display a floating-point number to two places, just use %.2f, and don't worry about explicitly rounding it yourself.
  • If you're trying to manipulate dollars and cents, it's often much easier to use integer math (representing cents) across the board.

round() for float in C++

It's available since C++11 in cmath (according to http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf)

#include <cmath>
#include <iostream>

int main(int argc, char** argv) {
std::cout << "round(0.5):\t" << round(0.5) << std::endl;
std::cout << "round(-0.5):\t" << round(-0.5) << std::endl;
std::cout << "round(1.4):\t" << round(1.4) << std::endl;
std::cout << "round(-1.4):\t" << round(-1.4) << std::endl;
std::cout << "round(1.6):\t" << round(1.6) << std::endl;
std::cout << "round(-1.6):\t" << round(-1.6) << std::endl;
return 0;
}

Output:

round(0.5):  1
round(-0.5): -1
round(1.4): 1
round(-1.4): -1
round(1.6): 2
round(-1.6): -2


Related Topics



Leave a reply



Submit