Link to home
Start Free TrialLog in
Avatar of zandy1
zandy1

asked on

math calculations

i am having trouble getting an int divide with another value and answer in 2 decimal places. here's the problem:
the user answered "05:04" as time so:
char ans[5]="05:04";
i need to convert the min=.04 to .07 of 60 mins (the fraction within the 60 min whole).
so the answer = "05.07"
now, i tried the following and it keeps giving me a 0 for an answer.  please help.
/*-------------------------*/
char ans[5]="05:04";
float value=0.0;
int h,m;

sscanf(ans,"%2d:%2d",&h,&m);
      value= h;
      if(m >=0 && m < 60)
            value= (value+(m/60));
/*----------------------------------------*/
the real problem is when i calculate (m/60), i get an answer of .0000000 instead of .066,  HOW DO I GET AN ANSWER WITH 2 DECIMAL PLACES?

zandy1
ASKER CERTIFIED SOLUTION
Avatar of sergelebel
sergelebel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozo
value += m/60.0;
sergelebel, that's not the way you do it... here is how,,,

sscanf(ans,"%.2f:%.2f",&h,&m);

Hope this helps...

-Viktor
--Ivanov
to get an ANSWER with 2 decimal places, you could say
  printf("value=%.2f\n",value);

 
  sscanf(ans,"%.2f:%.2f",&h,&m);
is meaningless. (i.e.: "undefined behavior")

  int h,m; sscanf(ans,"%2f:%2f",&h,&m);
is also undefined.
To use sscanf(ans,"%2f:%2f",&h,&m) you would have to declare
  float h,m;

but
  int h,m; sscanf(ans,"%2d:%2d",&h,&m);
works fine, as long as you use (m/60.0) instead of (m/60)