How to convert double to int in swift

后端 未结 8 1745
别跟我提以往
别跟我提以往 2020-12-24 10:30

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         


        
8条回答
  •  感情败类
    2020-12-24 10:47

    A more generic way to suppress (only) the .0 decimal place in a string representation of a Double value is to use NSNumberFormatter. It considers also the number format of the current locale.

    let x : Double = 2.0
    let doubleAsString = NumberFormatter.localizedString(from: (NSNumber(value: x), numberStyle: .decimal) 
    // --> "2"
    

提交回复
热议问题