Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.
Rounding off digits in C???
Name: Dado Date: September 21, 2003 at 12:37:03 Pacific OS: Win Me CPU/Ram: 512 MB
Comment:
When I compile my C program, the answers I get as an output are correct (ex. 10.0, 10.0, 10.0), but it does not show the correct digit after the decimal point. It is supposed to be 10.6, 10.4, 10.5 etc. How can I get those digits to show up for the output?? Help appreciated!
Name: M Date: September 21, 2003 at 13:42:07 Pacific
Reply:
#include <stdio> main() { float result; int i,j; i=25; j=10; result = i/j
printf("The result is: %f \n" ,result) } note the fractional part is discarded because the two operands were declared as integers, even though result has been declared as a float. To Force the answer to be promoted to a float then a cast can be used: result = (float) i/j; /*gives the result 2.5*/
Hope this is of some use. M
0
Response Number 2
Name: Dado Date: September 21, 2003 at 14:59:52 Pacific
Reply:
Thank you very much for answering. It works now perfectly. Who would think that such a simple addition of code could change everything.
Summary: I feel silly for asking this, but.. how do I round up decimals in C? My textbook asks me to use numbers rounded to 2 decimal places for my current project, but it has never explained how to do so. M...
Summary: Hello all! this is my first time at these forums. I am fairly new at c++. in a program of mine, i would either like to round off to a certain number of decimal places or just cut off decimal places....
Summary: hello, I'm just starting off with the C programming language, and i'm making a very simple program that contains two functions for inputing and outputing integers. I've started the first part, for...