Using mod operator in iOS app

谁说胖子不能爱 提交于 2019-12-12 08:33:41

问题


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?


回答1:


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.




回答2:


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

The operands of the % operator shall have integer type.




回答3:


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.)



来源:https://stackoverflow.com/questions/3731167/using-mod-operator-in-ios-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!