So I\'m trying to figure out how I can get my program to lose the .0 after an integer when I don\'t need the any decimal places.
@IBOutlet weak var numberOf
Swift 4 - Xcode 10
Use this code to avoid a crash if the double value exceeds the int boundaries (and so isn't representable):
Add this private extension to your class:
private extension Int {
init?(doubleVal: Double) {
guard (doubleVal <= Double(Int.max).nextDown) && (doubleVal >= Double(Int.min).nextUp) else {
return nil
}
self.init(doubleVal)
}
Use the extension in your class this way:
func test() {
let d = Double(123.564)
guard let intVal = Int(doubleVal: d) else {
print("cannot be converted")
}
print("converted: \(intVal)")
}