iOS objective-C:在浮点上使用模来从“英尺”获得“英寸”

我正在试图做一个简单的目标C高度转换器。 input是脚的(浮动)variables,我想转换为(int)英尺和(浮动)英寸:

float totalHeight = 5.122222; float myFeet = (int) totalHeight; //returns 5 feet float myInches = (totalHeight % 12)*12; //should return 0.1222ft, which becomes 1.46in 

但是,我不断从xcode中得到一个错误,我意识到模运算符只能使用(int)和(long)。 有人可以推荐一种替代方法吗? 谢谢!

即使modulo工程为浮动,使用:

fmod()

你也可以用这个方法

 float totalHeight = 5.122222; float myFeet = (int) totalHeight; //returns 5 feet float myInches = fmodf(totalHeight, myFeet); NSLog(@"%f",myInches); 

你为什么不使用

 CGFloat myInches = totalHeight - myFeet; 

正如前面所回答的,减法是要走的路。 只记得把十分之一英尺乘以12:

 float totalHeight = 5.122222; int myFeet = (int) totalHeight; float myInches = (totalHeight - myFeet) * 12;