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

10
  • 1
    Um, do you return the rounded value cast to void? Commented Sep 21, 2012 at 7:22
  • There is format is also available in c, then why you proceed with separate function, ie printf("%0.1f", a ); Commented Sep 21, 2012 at 7:23
  • @H2CO3 - Oh yes.. I return it back to main where I use it to process further. Commented Sep 21, 2012 at 7: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... Commented Sep 21, 2012 at 7:29
  • 2
    Note that not all decimal fractions are exactly representable as floats, so you will not always get the expected result. Commented Sep 21, 2012 at 7:34

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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
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'.
@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.
@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.
@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..
-1

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);
}

5 Comments

What is Math.pow? Is this JavaScript? Also, this is overly complicated. For example, that if for checking against rounding is completely unnecessary...
Sorry, yes, too much time doing C# I took on bad habits.
I can't seem to be able to delete my answers, strange.
@Swiss My point wasn't to be rude. This answer just doesn't really fit OP's needs.
Thanks all for your answers.. It was really a good knowledge for me.. Thanks all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.