# Newbie question: rounding numbers

• 02-03-2006
cantore
Newbie question: rounding numbers
Hi all, I'm fairly new to the C language and I'm having issues with rounding numbers.
I'm writing a program that asks the user to input a number (this number is a variable of type double).
The user can obviously enter any number, for example, 14.45675. What I'm trying to do is to get that number and round it off to two decimals (so it will be 14.46) to proceed with further calculations that are related with dollars/cents (hence the reason why I only want two decimal places).
In other words, I'm trying to round the number to two decimals and 'chop off' the rest of the decimals so they don't interfere later on with my calculations.
I hope this is clear enough.

• 02-03-2006
filker0
Are you trying to truncate (that is, chop off) or round to the nearest .01? Rounding of 0.999 gives 1.00, truncation gives 0.99.

Note that not all decimal fractions can be represented exactly by floating point values, and even after rounding and/or truncation, you might find some stray non-zero digits beyond your desired significant digits.
• 02-03-2006
cantore
Thanks for your quick reply filker. I'm trying to round to the nearest .01.
Any ideas?
• 02-03-2006
filker0
A few.

I have used a C compiler with a math package that included a set of "round()" functions that did precision rounding, but they were non-standard.

If you include <math.h>, you can use (when dealing with doubles) the expression
Code:

`double roundedval = round(oldval * 100.0) / 100.0;`
This will result in a rounding, as desired, within the limitations of the internal floating point representation.
• 02-03-2006
cantore
My book mentions the round() function too, and it gives an example, but they use the "tools.h" library which I don't seem to have ( I get a 'no such file or directory...' error message). I did try your suggestion, including the <math.h> but it tells me that round is an undeclared identifier. :(
• 02-03-2006
filker0
math.h is a standard C header.

When you use it, on Linux (and other Unix varients) you must also use the "-lm" switch when linking, as in
Code:

`gcc -o example example.c -lm`
This makes it include referenced functions from libm.a (or the equivilent).

If your compiler package does not supply math.h and the math library, I'm surprised.

However, you can write your own round() function:
Code:

```double myround(double value, int places) {     double  adjust = 1.0;     long temp;     for (;places > 0; places--)     {         adjust *= 10;     }     temp = (long)((value * adjust) + 0.5);     return (double)temp / adjust; }```
Note that this function may not have exactly the same results as using round(), but it should give you good values. I've not tested this code -- I just typed it in here -- but I think that it's correct. I've not commented it, so see if you can figure out what it does and how it does it. :cool:
• 02-04-2006
Salem
Don't use floats for money amounts, use an integer to count the number of cents.
When you want to display an amount, use %100 to extract cents and /100 to extract dollars.

http://en.wikipedia.org/wiki/Floating_point
• 02-04-2006
cantore
Thanks everybody for the help, after a few headaches I think I got it. Thanks again :)
• 02-04-2006
Rashakil Fol
round was added in C99, so I wouldn't be surprised if some compilers didn't support it.
• 02-04-2006
dwks
I think printf() automatically rounds floating point numbers:
Code:

`printf("%.2f\n", 1.249);`
Suspected output:
Code:

`1.25`
• 02-04-2006
dwks
Here's an implemetation of round:
Code:

```double newround(double x) {     return (int)(x + .5); }```