设置价值限制,iPhone
我有以下代码:
float valueCalculated = (val1 / val2) * 100;
我想要做的是将 valueCalculated 的最大值限制为 100。
我相信我可以使用某种 if 语句来做到这一点,但这意味着更多的代码行。编辑//事实并非如此,请参阅下面的答案。
谢谢,
斯图
I have the following code:
float valueCalculated = (val1 / val2) * 100;
What I want to be able to do is to cap the maximum value of valueCalculated to 100.
I believe I could do this using some sort of if statement, but this would mean many more lines of code. Edit// This is not the case, see the answers below.
Thanks,
Stu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你的意思是像这样“更多行代码”?
You mean "many more lines of code" like this?
但我想一个函数会更好,你可以使用函数或简单的宏来完成:
我想我更好地理解你的问题:
希望
它有帮助,
乔
but I guess a function would be better, you can do with function or a simple macro:
I guess I understood better your question:
where
hope it helps,
Joe
如果我理解这个问题,那么
……应该按照你的要求去做。
但是,您是否担心负值,即 val1 或 val2 可能为负数,并且您是否要将 valueCalculated 的可能值限制为 0-100?如果是这样,那么您可能想要...
If I understand the question then...
...should do what your asking.
However, are you worried about negative values, i.e. could val1 or val2 be negative and do you want to limit possible values for valueCalculated to 0-100?. If so then you might want...
这个怎么样:
What about this:
我不是 iphone 开发人员,但 modulo 不是您要找的吗?
?
I'm not an iphone dev, but isn't modulo what you're looking for?
?
如果您只想通过“加权”将计算值限制在 0 到 100 之间,则可以将该值除以可能的最大值,然后乘以 100。这假设该值不是负数。
If you just want to limit a calculated value to 0 to 100 by "weighting" it, you would divide that value by the maximum that it could be and then multiply by 100. This assumes the value isn't negative.