Stefan Talevski
Stefan Talevski

Reputation: 15

Hide / Remove .0 decimals from UISlider - Swift 4

I am newbie to Swift 4 / Xcode development still learning almost 2 weeks, and I have problem with hiding / removing .0 from the slider value that I am trying to create. I have attached image and the code that its displayed on the app.What I am trying to do with this code is to show the number on the app without .0 like if its 15000.0 to show only 15000

    @IBAction func changeValueCredit(_ sender: Any) {
     let fixed = roundf((sender as AnyObject).value / 100) * 100;
     (sender as AnyObject).setValue(fixed, animated: true)
     inputValue.text = "\(creditNumber.value)" + " $ "
}

image from the slider and the value

Thank you all

Upvotes: 1

Views: 1291

Answers (2)

Guilherme Matuella
Guilherme Matuella

Reputation: 2273

fixed is a Float value, so it won't truncate to your desired value. If you don't want to round it up or down, you can call:

@IBAction func changeValueCredit(_ sender: Any) {
     let fixed = roundf((sender as AnyObject).value / 100) * 100;
     let truncatedFixed = Int(fixed)
     (sender as AnyObject).setValue(truncatedFixed, animated: true)
     inputValue.text = "\(creditNumber.value)" + " $ "
}

Upvotes: 0

Kamran
Kamran

Reputation: 15248

Convert to Int to get rid of decimal numbers,

 inputValue.text = "\(Int(creditNumber.value))" + " $ "

Upvotes: 1

Related Questions