How to convert double to int in swift

后端 未结 8 1701
别跟我提以往
别跟我提以往 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"
    
    0 讨论(0)
  • 2020-12-24 10:48

    Swift 4 - Xcode 9

    let value = doubleToInteger(data:"ENTER DOUBLE VALUE")
    
    func doubleToInteger(data:Double)-> Int {
                let doubleToString = "\(data)"
                let stringToInteger = (doubleToString as NSString).integerValue
    
                return stringToInteger
            }
    
    0 讨论(0)
  • 2020-12-24 10:49

    You can use:

    Int(yourDoubleValue)
    

    this will convert double to int.

    or when you use String format use 0 instead of 1:

    String(format: "%.0f", yourDoubleValue)
    

    this will just display your Double value with no decimal places, without converted it to int.

    0 讨论(0)
  • 2020-12-24 10:50
    extension Double {
      var prettyWeight: String {
        Int(exactly: self) == nil ? "\(self)kg" : "\(Int(self))kg"
      }
    }
    

    test result

    for i in stride(from: 0.5, to: 10, by: 0.5) {
      print("\(i): \(i.prettyWeight)")
    }
    
    0.5: 0.5kg
    1.0: 1kg
    1.5: 1.5kg
    2.0: 2kg
    2.5: 2.5kg
    3.0: 3kg
    3.5: 3.5kg
    4.0: 4kg
    4.5: 4.5kg
    5.0: 5kg
    5.5: 5.5kg
    6.0: 6kg
    6.5: 6.5kg
    7.0: 7kg
    7.5: 7.5kg
    8.0: 8kg
    8.5: 8.5kg
    9.0: 9kg
    9.5: 9.5kg
    
    0 讨论(0)
  • 2020-12-24 11:03

    that should work:

    // round your double so that it will be exactly-convertible
    if let converted = Int(exactly: double.rounded()) {
      doSomethingWithInteger(converted)
    } else {
      // double wasn't convertible for a reason, it probably overflows
      reportAnError("\(double) is not convertible")
    }
    

    init(exactly:) is almost the same with init(:), the only difference is that init(exactly:) doesn't crash while init(:) may call fatalError(:) in case of failure.

    You can check their implementations here

    • init(:)
    • init(exactly:)
    0 讨论(0)
  • 2020-12-24 11:03

    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)")
    }
    
    0 讨论(0)
提交回复
热议问题