-5

I am facing a problem where I am not getting values of decimal places here is the code that I used to run in Swift Playground

print(100/1000)
print(Float(100/1000))

Expected Output :

0.10
0.10

Actual Output:

0
0.0

enter image description here

Varun Naharia
  • 5,318
  • 10
  • 50
  • 84
  • 3
    You're doing integer divisions, you're getting integer result – Cid Sep 11 '20 at 12:45
  • Think: `print(100/1000); print(Float(100/1000)); print(Float(100)/1000); print(100/Float(1000)); print(Float(100)/Float(1000))`. 5 "possibilities". Integer can't have decimal, right? If so, Float(someInteger), where `someInteger` is `100/1000`, and you got before 0, that's normal to have 0.0, because that's `Float(0)`, no? – Larme Sep 11 '20 at 12:46

2 Answers2

0

Because of this:

let variable = 100/1000
print(type(of: variable))
// prints Int
Roman Ryzhiy
  • 1,540
  • 8
  • 5
0

Just do the following:

print(100.0 / 1000.0)
print(Float(100) / Float(1000))

So in order to get a floating point result you need to divide floating point numbers instead of two integers

finebel
  • 2,227
  • 1
  • 9
  • 20