0

In my application im resseting one variable(Flow) to Zero every 12 hrs. For that i have one logic but it doesnt work as expected. TIme i get is in hour format. When the time reached 12, the variable Flow become zero but the next second it shoud again start to read the current value. But from my logic, it starts to read 13th hour.

THis is sample code

int _tmain(int argc, _TCHAR* argv[])
{

    double dtime;
    int iTime;
    double flow = 2.000;
    printf("Enter time: ");
    scanf(" %lf",&dtime);

    iTime = (int)dtime;

    if(iTime% 12==0)
    {
        flow=0;
    }

    printf("Time: %d\n",iTime);
    printf("Flow: %lf",flow);
    getch();
    return 0;
}

IF the time 11.3614 like that, the Flow is 2. IF the time reached exact 12, then for that second, Flow =0. Then again the next second it starts to read the Flow value. Here the flow value is continuous reading from other functions. But here in this sample code,i hard coded for understanding.

if the time is 12.023, when i convert the double to int, then it returns 12 only. But as per my requirement, this time it starts to read the value. How can i convert double to int without missing the fractions?

I tried

iTime = (int)(dtime+0.5);

But again ,iTime become 13 only dtime is more than 12.5.

Anu
  • 905
  • 4
  • 23
  • 54

1 Answers1

0

You can first multiply double by some factor (10, 100 or 1000), cast it to int, then divide it by the same factor back to double.

I don't understand exactly the logic, but you cannot save the fraction during conversion to int, since int values cannot have it.

Also try to look here: Can't use modulus on doubles?

Timofeus
  • 181
  • 1
  • 9