1

I have written a C program (which is part of my project) to round off a float value to the given precision specified by the user. The function is something like this

     float round_offf (float num, int precision)

What I have done in this program is convert the float number into a string and then processed it.

But is there a way to keep the number as float itself and implement the same.

Eg. num = 4.445 prec = 1 result = 4.4

xxbbcc
  • 16,930
  • 5
  • 50
  • 83
user1611753
  • 355
  • 1
  • 4
  • 9
  • 1
    Um, do you return the rounded value cast to `void`? –  Sep 21 '12 at 07:22
  • There is format is also available in c, then why you proceed with separate function, ie printf("%0.1f", a ); – Riskhan Sep 21 '12 at 07:23
  • @H2CO3 - Oh yes.. I return it back to main where I use it to process further. – user1611753 Sep 21 '12 at 07:28
  • @user1611753 you clearly didn't get the point of my comment... If you want to return the value, you can't declare your function as `void`... –  Sep 21 '12 at 07:29
  • @kTekkie - The value of precision varies in dynamically and the format specifier which you have said cannot be used for all values of precision – user1611753 Sep 21 '12 at 07:29
  • @user1611753 [You can specify dynamic precision formats for printf.](http://stackoverflow.com/questions/9627075/dynamic-float-format-specifier-in-c) –  Sep 21 '12 at 07:32
  • 1
    Note that not all decimal fractions are exactly representable as floats, so you will not always get the expected result. – unwind Sep 21 '12 at 07:34
  • @unwind yes, right. We all hate floating-point arithmetic, don't we? –  Sep 21 '12 at 07:34
  • possible duplicate of [Rounding Number to 2 Decimal Places in C](http://stackoverflow.com/questions/1343890/rounding-number-to-2-decimal-places-in-c) – AProgrammer Sep 21 '12 at 08:00
  • @AProgrammer - No it is not. Here we are implementing a function seperately to do so.Application of both are differant – user1611753 Sep 21 '12 at 09:23

2 Answers2

4

Of course there is. Very simple:

#include <math.h>

float custom_round(float num, int prec)
{
    int trunc = round(num * pow(10, prec));
    return (float)trunc / pow(10, prec);
}

Edit: it seems to me that you want this because you think you can't have dynamic precision in a format string. Apparently, you can:

int precision = 3;
double pie = 3.14159265358979323648; // I'm hungry, I need a double pie
printf("Pi equals %.*lf\n", precision, pie);

This prints 3.142.

  • 1
    be careful by calling the function `round`: you need to include `math.h` for `pow()`, which makes your `round` clash with one from `math.h` – Andreas Fester Sep 21 '12 at 07:28
  • This doesn't do the right thing for negative `num`. One fix would be to use `... - 0.5` instead of `... + 0.5` for negatives. Another would be to make use of 'round' (if C99 can be assumed) or 'floor'. – Mark Dickinson Sep 21 '12 at 08:39
  • 1
    @MarkDickinson again: it depends on what you consider 'correct'. There's no such thing as 'correct rounding'. There is rounding towards positive infinity (which this approach does), rounding towards negative inifinity, rounding away from zero (which your method proposes), rounding towards zero, banker's rounding, etc. All of them are correct from the point of view of a certain logic. –  Sep 21 '12 at 08:42
  • @H2CO3. No: I'm talking about the fact that `custom_round(-2.2, 0)` returns `-1.0`. Surely that can't be what you intended. – Mark Dickinson Sep 21 '12 at 08:49
  • @H2CO3 - I just answered for the suggestion which he made for using output format specifiers. Anyway this point was also helpful. But what I intended was your first reply which was what I needed. Thank you once again.. – user1611753 Sep 21 '12 at 09:19
0

Yes:

float round_offf(float num, int precision)
{
  int result;
  int power;

  power = pow(10, precision + 1);
  result = num * power;
  if ((result % 10) > 5)
    result += 10;
  result /= 10;
  return ((float)result / (float)power);
}
Eregrith
  • 4,263
  • 18
  • 39