Search code examples
swiftmacosnsslider

Increment NSSlider by 0.1? macOS, Swift


I have a NSSlider where the minimum is 0.6 and maximum is 1.

I want to be able to increment by 0.1 and not have values such as 0.6493 or 0.8435

How would I accomplish this?

I have the function

@IBAction func slider(sender : NSSlider) {
    var x: Double = sender.doubleValue
    // do something
}

Thanks.


Solution

  • Try this format :

    @IBAction func slider(sender : NSSlider) {
    
      let value = sender.value
    
      // "%.1f" show how many decimals you want to show
      let formatted = String(format: "%.1f", value)
    
      //formatted will be printed 0.X
    
    }