-
Rounding Floats
Hi, I was wondering if anybody knows an easy way to round floats. I wrote a program calculating money, and I'd like to display the right amount of decimal places for dollars and cents. Right now, it's printing numbers like "156.945957" and I'd just like it to print "156.95". Thanks in advance.
-
if you want to round to a certain decimal amount every time, you can just use the printf conversion.
[example]
printf("Random number = %.3f ", floatingnum);
[\example]
-
you can use printf and specify how much to round up by with %.Nf where N represents how far you want to round off.
i.e. %.2f will round off to 2 decimal places. %.3f will go to 3 decimal places and so on.
-
-
Whatabout rounding float and stores it in float variables?
Normally I write my own function to do this, but any instant functions else?
-
Have a look at the functions floor and ceil in math.h