Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Int to Double in Swift

Tags:

ios

swift

People also ask

Can you convert Int to Double?

We can convert int value to Double object by instantiating Double class or calling Double. valueOf() method.

How do you make a Double Int in Swift?

We can round a double to the nearest Int by using the round() method in Swift. If the decimal value is >=. 5 then it rounds up to the nearest value. for example, it rounds the 15.7 to 16.0 .

How do you make a variable Double in Swift?

Before the equals sign, put a colon and the variable type like this: var value: Double = 3 . This explicitly tells Swift that the variable is a double, even though the value looks like an integer.

Can you add Int and Double in Swift?

As a result of all this, Swift will refuse to automatically convert between its various numeric types – you can't add an Int and a Double , you can't multiply a Float and an Int , and so on.


Try Double(theOrder.bookingMins)


What I prefer to do is to use computed properties. So I like to create an extension of Double and than add a property like below:

extension Int {
  var doubleValue: Double {
    return Double(self)
  }
}

And than you can use it in very Swifty way, I believe in future updates of swift language will be something similar.

let bookingMinutes = theOrder.bookingMins.doubleValue

in your case

label.text = String(format: "%.1f hour", bookingMinutes / 60.0) 

Style guide used: https://github.com/raywenderlich/swift-style-guide


label.text = String(format:"%.1f hour", Double(theOrder.bookingMins) /60.0)

Swift 5 (Basic to Convert Int to Double)

let a = 3, let b =4

func divide ( number1 : Int, number2 : Int)
{
let divide = Double (number1) / Double (number2)
print (divide)

}

The output should be 0.75