如何找到列表中最接近的值,这将返回更高的值?示例:[3,7,12,19] 列表,如果我的值为 8,如何获得最接近(较大)的值 12?我想要在 Dart 中使用这个逻辑。
只需过滤仅List大于或等于您的数字的值并获取最低值:
var n = 8; // Number to match
var l = [3, 7, 12, 19]; // List of values
var greater = l.where((e) => e >= n).toList()..sort(); //List of the greater values
print(greater.first); // Print the first value. -> 12
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3196 次 |
| 最近记录: |