Connecting Tech Pros Worldwide Help | Site Map

hours

Newbie
 
Join Date: Oct 2006
Posts: 5
#1: Oct 21 '06
Hi, i need help in c prog not c ++ so please don't tell me c ++ code :D

m_hrs is 8.

hrs is double

hrs= (m_hrs * 20) / 60;

printf(" hrs is %.2lf", hrs);

ans: 2.00 not 2.67

the problem is i am not getting 2.67 but when i calculated with calculator i am getting 2.667.

Thanks for help
arne's Avatar
Expert
 
Join Date: Oct 2006
Posts: 306
#2: Oct 21 '06

re: hours


Quote:

Originally Posted by muna123

Hi, i need help in c prog not c ++ so please don't tell me c ++ code :D

m_hrs is 8.

hrs is double

hrs= (m_hrs * 20) / 60;

printf(" hrs is %.2lf", hrs);

ans: 2.00 not 2.67

the problem is i am not getting 2.67 but when i calculated with calculator i am getting 2.667.

Thanks for help

You do the calculation (m_hrs * 20) / 60 with integers (I guess m_hours has type int?) and cast the result to double. You can either define m_hrs to be of type double or change your code to

Expand|Select|Wrap|Line Numbers
  1. hrs = ((double)m_hrs*20/60);
  2.  
Newbie
 
Join Date: Oct 2006
Posts: 5
#3: Oct 21 '06

re: hours


Quote:

Originally Posted by arne

You do the calculation (m_hrs * 20) / 60 with integers (I guess m_hours has type int?) and cast the result to double. You can either define m_hrs to be of type double or change your code to

Expand|Select|Wrap|Line Numbers
  1. hrs = ((double)m_hrs*20/60);
  2.  


Thanks it worked !!!
arne's Avatar
Expert
 
Join Date: Oct 2006
Posts: 306
#4: Oct 21 '06

re: hours


Quote:

Originally Posted by muna123

Thanks it worked !!!

I hope you have understood why? :)
Reply