0

Background:
I have an array of integer times given as 0830 for 08:30, 1745 for 17:45, etc. I need to calculate the time difference between times. So I'm first turning integer times into floating point times by dividing by 100.0. This means that 0830 will become 8.30.

int inTime = 0830, outTime = 1745;
float newIntime = inTime/100.0;
float newOutTime = outTime/100.0;   

The next step, and this is my question, is: How do I divide the decimal part by 0.6 so I get 8.50. This is the only way I'll be able to subtract/add times and get the correct time difference in a meaningful format.

I have not been able to figure out (or look up) a way to multiply the decimal part, i.e. to "access" only what's on the right side of the decimal point. Any help will be appreciated!

WykoW
  • 181
  • 5
  • do you want to convert it in hours and then manipulate it – Code Warrior Jul 25 '15 at 08:10
  • possible duplicate of [Extract decimal part from a floating point number in C](http://stackoverflow.com/questions/499939/extract-decimal-part-from-a-floating-point-number-in-c) – m.s. Jul 25 '15 at 08:10
  • Sometimes you just have to go back to the drawing board. – Potatoswatter Jul 25 '15 at 08:15
  • 3
    Just don't. Store an integer number of minutes instead (or use a standard representation such as std::tm). – Oliver Charlesworth Jul 25 '15 at 08:23
  • Many great answers already. I'll give them a try and see which one is the best solution. I should mention that precision is less of a concern in this case. – WykoW Jul 25 '15 at 08:49
  • @Volodya - Yes, 0800, 0801, etc.... are causing problems as they're apparently interpreted as octal base numbers. Unfortunately that is how the numbers are stored! – WykoW Jul 25 '15 at 08:53
  • why don't extract the decimal and int parts separately: `newIntime_h = inTime/100; newIntime_m = inTime % 100;` – phuclv Jul 25 '15 at 10:28
  • 1
    *"Unfortunately that is how the numbers are stored!"* - What exactly do you mean by "stored"? Storage and textual representation are two different things. – Christian Hackl Jul 25 '15 at 10:34

5 Answers5

2
float intPart = floor(newInTime);
float converted = intPart + (newInTime - intPart) / 0.6;

And it's better to not use float for this purpose. See others' answer.

xuhdev
  • 8,018
  • 2
  • 41
  • 69
  • This method works just fine for integers below 0800 and 1000 or above. It doesn't work with integers between 0800 and 0999 because they're read as octal base which really messes up calculations. – WykoW Jul 25 '15 at 09:29
  • @WykoW how do you have 0830 as an octal base number? My understanding of your question is you read the strings as decimal numbers. – xuhdev Jul 25 '15 at 16:02
  • Actually, I found out that reading the numbers (stored as strings) into my program via ifstream stripped the leading zeros, in which case the octal base problem does not exist anymore. And then your solution works just fine. – WykoW Jul 26 '15 at 17:13
2

Don't convert to float in the first place. Use modulus to extract the minutes:

int inTime = 830;
int inHours = inTime / 100;
int inMinutes = inTime % 100;
int inTotalMinutes = inHours * 60 + inMinutes;

Also, 0830 is not a valid literal. The leading zero causes it to be interpreted as an octal number, and 8 is not a valid octal digit.

Max Shawabkeh
  • 37,799
  • 10
  • 82
  • 91
  • Do not use float is the only sensible answer to this question. The time (in minutes) is an integral value, so use integers. – john Jul 25 '15 at 10:14
0

You do not want to do this in float, because you will be potentially losing precision.

I would suggest doing something along the line of:

// or use std::tm per Potatoswatter's comment
struct Time
{
  int hours;
  int minutes;
  Time(int time)
  {
    hours = time/100;
    minutes = time-hours*100;
  }
};


Time operator-(const Time& a, const Time& b)
{
  // You could also just return these minutes and be done with it
  int minutes (a.hours-b.hours)*60 + (a.minutes-b.minutes);
  Time result;
  result.hours = minutes/60;
  result.minutes = minutes-result.hours;
}

int main()
{
  Time inTime(830), outTime(1745);
  Time result = inTime-outTime;
  // ...
  return 0;
}

Another thing to keep in mind, is that you should not write:

Time inTime(0830);

because the compiler will think that you are talking about octal base (and it cannot have the 8 as a digit).

v010dya
  • 5,296
  • 7
  • 28
  • 48
0

It is easier and more reasonable to convert 08:30 to 8.5 rather than 8.3 for floating point calculation. It is 8.5 hours from 0:00. Convert back is also easy. Hour is whatever to the left of decimal point and minute is whatever to the right of decimal point multiple by 60.

simon
  • 391
  • 1
  • 9
-1

Extract it with modf. That should eliminate the extra loss of precision.

EvilTeach
  • 28,120
  • 21
  • 85
  • 141