8

I have an NSTimeInterval that is stored as a double. I would like to get the amount of minutes that are inide of the second value using the % operator.

minutes = secondValue % 60;

where minutes is declared as double minutes

The result is that XCode says "Invalid operands to binary %"

Thoughts?

Jerry
  • 537
  • 1
  • 9
  • 16
  • See http://stackoverflow.com/questions/1237778/how-do-i-break-down-an-nstimeinterval-into-year-months-days-hours-minutes-and – martin clayton Sep 16 '10 at 22:12
  • 3
    Besides getting the modulo to work, doesn't this give the opposite result of what you want? If `secondValue` is 123 (which works out to 2 minutes, 3 seconds), the result of `secondValue % 60` will be 3. It seems like this should be division. – Chuck Sep 16 '10 at 22:13

3 Answers3

13

From the C standard, section 6.5.5 Multiplicative operators, paragraph 2:

The operands of the % operator shall have integer type.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • This answer makes no sense since the OP edited the question. Whether or not that's cause for a downvote, I'm not sure. – Carl Norum Mar 01 '12 at 18:54
12

The OP changed their question, so here is my new answer:

You want to do minutes = floor(secondsValue) / 60; You want int division, not modulus.

AlcubierreDrive
  • 3,654
  • 2
  • 29
  • 45
4

If secondValue is a float or double, you can use C type casting:

minutes = ((int)secondValue) % 60;

EDIT:

(If secondValue is too big to fit in an int, this won't work. So you need to know that the range of your value is appropriate.)

hotpaw2
  • 70,107
  • 14
  • 90
  • 153