iOS目标-C:在浮点数上使用modulo从英尺得到"英寸"

jak*_*115 10 c objective-c modulo ios

我正在尝试制作一个简单的物镜-C高度转换器.输入是一个脚的(浮点)变量,我想转换为(int)feet和(float)inches:

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

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

Ano*_*dya 26

即使模数适用于浮点数,使用:

fmod()

你也可以用这种方式......

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