Swift3 获取介于0到1之间的整数

Swift3 获取介于0到1之间的整数,swift3,Swift3,我有一个UISlider,它可以生成0到1之间的数字 0.0590829 0.0643739 .. 我想得到它们之间的整数,比如: 0.1 0.2 0.3 ... 1.0 发现(在c中): 但它在swift上不起作用 var x = arc4random() % 11 * 0.1; //error: binary operator '*' cannot be applied to operands of type 'UInt32' and 'Double' 谢谢 乘以10得到介于0.0和1

我有一个UISlider,它可以生成0到1之间的数字

0.0590829
0.0643739
..
我想得到它们之间的整数,比如:

0.1
0.2
0.3
...
1.0
发现(在c中):

但它在swift上不起作用

var x = arc4random() % 11 * 0.1;
//error: binary operator '*' cannot be applied to operands of type 'UInt32' and 'Double'
谢谢

  • 乘以10得到介于0.0和10.0之间的值
  • 四舍五入删除小数点
  • 除以10
例如:

let values = [0, 0.0643739, 0.590829, 0.72273, 1]

for value in values {
    print("\(value) -> \(round(value * 10) / 10)")
}

// 0.0 -> 0.0
// 0.0643739 -> 0.1
// 0.590829 -> 0.6
// 0.72273 -> 0.7
// 1.0 -> 1.0

不知道为什么人们投票认为这是“太广泛”。毫无疑问,缺乏努力。你的代码在哪里?你试过什么?不要只要求别人为你做这项工作。如果你展示你所做的尝试,并证明你花时间尝试帮助自己,你会得到更多更好的答案。看@AshleyMills你完全正确,修正了
let values = [0, 0.0643739, 0.590829, 0.72273, 1]

for value in values {
    print("\(value) -> \(round(value * 10) / 10)")
}

// 0.0 -> 0.0
// 0.0643739 -> 0.1
// 0.590829 -> 0.6
// 0.72273 -> 0.7
// 1.0 -> 1.0