gpt4 book ai didi

iOS objective-C : using modulo on a float to get "inches" from Feet

转载 作者:技术小花猫 更新时间:2023-10-29 10:57:28 26 4
gpt4 key购买 nike

我正在尝试制作一个简单的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com