How to make a simple Double by Int split in Swift?

Looking at various posts on this topic, but still no luck. Is there an easy way to do division / conversion when dividing Double (or Float) by Int? Here is a simple example of the return of the playground and the error "Double does not convert to UInt8."

var score:Double = 3.00 var length:Int = 2 // it is taken from some an array lenght and does not return decimal or float var result:Double = (score / length ) 
+5
source share
2 answers

Passing int to double using var result:Double=(score/Double(length)) What this will do, before calculating the division, it will create a new double variable with int inside parentheses, hence there will be a constructor similar to the syntax.

+5
source

You cannot combine or use different types of variables together. You need to convert them all to the same type so that you can separate them. The easiest way that I see this happen is to make Int Double. You can do this quite simply by adding ".0" to the end of the whole you want to convert.

In addition, FYI: Floats are rarely used, so if you do not use them for something specific, its also just more liquid to use the more common variables.


+1
source

Source: https://habr.com/ru/post/1215655/


All Articles