Baker2795 - 3 months ago 12

Swift Question

How would I go about doing this? the code I have is

`let myInt = Int(currentSaleLabel.text!)`

let sale1Number = (myInt! - 1 + 100) % 100

let sale2Number = (sale1Number - 1 + 100) % 100

let sale3Number = (sale2Number - 1 + 100) % 100

let sale4Number = (sale3Number - 1 + 100) % 100

let sale5Number = (sale4Number - 1 + 100) % 100

let sale6Number = (sale5Number - 1 + 100) % 100

let sale7Number = (sale6Number - 1 + 100) % 100

let sale8Number = (sale7Number - 1 + 100) % 100

let sale9Number = (sale8Number - 1 + 100) % 100

let sale10Number = (sale9Number - 1 + 100) % 100

This works fine for numbers 0-99 but I need to eliminate the number 0 also.

Answer

One approach is to make a limit of 99 with modulo `%`

operator, like this:

```
let sale1Number = (myInt! - 1 + 100) % 100
```

This decrements `myInt`

, and wraps it to 99 when it gets negative. Adding 100 prior to applying `%`

has no effect on numbers in range 0..99. Negative numbers above -100 will get processed correctly.

I am actually also trying to avoid the number 0

You can use a modified formula, like this:

```
let sale1Number = (myInt! - 2 + 100) % 100 + 1
```