我正在尝试制作一个简单的 Objective-C 高度转换器。输入是英尺的 (float) 变量,我想转换为 (int) 英尺和 (float) 英寸:
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)。有人可以推荐一种替代方法吗?谢谢!
最佳答案
即使模数也适用于 float ,使用:
fmod()
你也可以用这种方式...
float totalHeight = 5.122222;
float myFeet = (int) totalHeight; //returns 5 feet
float myInches = fmodf(totalHeight, myFeet);
NSLog(@"%f",myInches);
关于iOS objective-C : using modulo on a float to get "inches" from Feet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16394216/