swift - 从最接近但小于另一个值的数组中获取值
问题描述
我有以下数组:
let array = [0.0, 4.5, 5.6, 16.4, 16.7]
给定一个随机数,找出数组中的哪个值小于但最接近。
例子
Value = 5.5 //result should be 4.5 from array
Value = 5.7 //result should be 5.6 from array
Value = 0.1 //result should be 0.0 from array
Value = 200.0 //result should be 16.7 from array
到目前为止,我有这段代码可以测试一个很大的数字,但不是一个最接近的较小数字
var resultNext = 100.0
if let nextResult = self.pointDataForActivity.first(where: { $0 > Value }) {
print("The first greater number is \(nextResult).")
resultNext = nextResult
}
解决方案
推荐阅读
- r - 在 R 中创建 Fama French 3 因素回归循环
- java - 如何在 Dynamics 365 Finance and Operations 中删除具有特殊字符的 DataEntity 条目
- python - 在 VM 上运行浏览器(非无头)
- javascript - 您能否解释一下为什么我们需要检查“DELETE”请求的状态是否为 200,以及为什么我们不必对“PUT”请求进行检查?
- swift - UICollectionViewListCell 附件顶部尾随放置
- sql - X 轴条形图 Grafana 上的标签
- php - Smarty::$_tpl_vars - 未定义属性 - Smarty 3 中支持 _tpl_vars
- javascript - 在 JavaScript 中混合 zlib 和 fflate 进行压缩?
- reactjs - Redux Toolkit - 使用 Axios 链接依赖的 post 请求
- apache-spark - Pyspark 将字符串转换为 UTC 时间戳 [获取 null]