Computing.Net > Forums > Programming > Rounding off digits in C???

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???

Reply to Message Icon

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!



Sponsored Link
Ads by Google

Response Number 1
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.


0

Response Number 3
Name: Infinite Recursion
Date: September 21, 2003 at 23:16:06 Pacific
Reply:

"Who would think that such a simple addition of code could change everything. "


I think that.
I put one semi-colon where needed in a multi-thousand line program that was DOA and it worked perfectly after the compile.

IR


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Program Shortcut VB6 - Program Starts a Pr...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Rounding off digits in C???

Rounding up decimals in C? www.computing.net/answers/programming/rounding-up-decimals-in-c/7181.html

Rounding in c++ www.computing.net/answers/programming/rounding-in-c/7129.html

Newbie in C www.computing.net/answers/programming/newbie-in-c/5513.html