-3

I have UISlider that produce numbers between 0 to 1,

0.0590829
0.0643739
..

I want to get the rounded number between them, like:

0.1
0.2
0.3
...
1.0

found this (in c):

float x = arc4random() % 11 * 0.1;

but its not working on swift

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

Thanks

Community
  • 1
  • 1
Marry G
  • 377
  • 1
  • 3
  • 16
  • Not sure why people are voting this as "Too broad". Lack of effort shown, for sure. – Ashley Mills Feb 28 '17 at 10:43
  • 1
    Where's your code? What have you tried? Don't just ask people to do the work for you. You'll get more and better answers If you show what you've tried, and demonstrate that you’ve taken the time to try to help yourself. See [Ask] – Ashley Mills Feb 28 '17 at 10:44
  • 1
    @AshleyMills You are absolutely right, fixed – Marry G Feb 28 '17 at 11:03

1 Answers1

0
  • Multiply by 10 to get values between 0.0 and 10.0
  • round to remove the decimal
  • divide by 10

Example:

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
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160