Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

double to int - please help

Status
Not open for further replies.

nappaji

Programmer
Mar 21, 2001
76
US
This is a samll program

main()
{
double a = 0.01;
doubel b = 0.01;
int i;

i = a/b;

printf ("%d", i);

}


When I print out i, it prints 0 instead of 1. What I suspect is since its a double / double division, there might be a precision problem. How do I conver the output of the division to an integer??? I want i to be declared an integer.

The precision problem might give out an i value of 0.99988 or something like that.

Thanks
 
The only thing I can see wrong with it is that you've spelt double wrong - "doubel". I compiled and ran it with the correct spelling and it was OK!.








Denster
 
To avoid the warning for converting double to int,you can use the cast operator "( )" like this:

#include <stdio.h>
void main()
{
double a = 0.01;
double b = 0.01;
int i;

i = (int)(a/b);

printf (&quot;%d &quot;, i);
}
 
I think that is a precision problem.

To avoid it try:

i=a/b+0.0000001;
hnd
hasso55@yahoo.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top