Hello.
I just started learning Swift. I'm going step by step. There is one point I don't understand.
Can anyone assist on this?
From line 98 I defined two numbers (number10 and number11)
These numbers (I am giving an example) will be used as Integers in the whole software. But I need to use it as double(float) in some places.
But the process I did between 104-105;
When I print at 106-107, I want to see the result as a double (or float).
So 9/5= 1.8 but I always get the result as 1.0 when I print it. Here the result is float but I can't see the fraction. how should i do it so i see the result as 1.8?
My most important request here is that these numbers should remain Integer in all my work, and only in this operation between 104-105 and output between 106-107 should be specially converted to Float (Double).
Thank you for your help in advance.
tongjiew
(Tongjie Wang)
2
A division between two integers will always result in an integer (the decimal part will be discarded). In your example, although 9 / 5 = 1.8, this is integer division so the ".8" is discarded and the result is 1. If you want to temporarily perform a double/float division, you can cast operands to Double or Float. A modified version would be
var number10 = 9
var number11 = 5
var result = Double(number10) / Double(number11)
print(result) // will print 1.8
Notice that you cannot do number10 /= Double(number11), because number10 can only hold integer
2 Likes