gpt4 book ai didi

iOS : round off to nearest number

转载 作者:行者123 更新时间:2023-11-28 19:56:40 26 4
gpt4 key购买 nike

假设我在关注 NSInteger:

  • 111
  • 246
  • 99
  • 82
  • 92
  • 85

是否有一个函数可以像这样转换(四舍五入)这些数字:

  • 110
  • 250
  • 100
  • 80
  • 90
  • 85

最佳答案

查看您要求的结果,小于 100 的数字四舍五入到最接近的 5,超过四舍五入到最接近的 10。这样,您可以得到想要的结果

- (NSInteger)roundToNearest:(NSInteger)inputNum
{
if (inputNum < 100){
return roundf(inputNum / 5.0f) * 5;
}
else {
return roundf(inputNum / 10.0f) * 10;
}
}

关于iOS : round off to nearest number,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26118034/

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