1

I'm trying to get the decimals in simple division but is always returning zero:

(lldb) po Double(20 / 100)
0.0

(lldb) po Float(20 / 100)
0.0

Any of you knows why is returning zero? or there is a way to get the of 0.20 ?

I'll really appreciate your help

user2924482
  • 8,380
  • 23
  • 89
  • 173

1 Answers1

4

20 / 100 is an integer division, and so is 0 (since 100 > 20). That 0 is passed to Double.init.

You want floating point division:

20.0/100.0

You can also use 20.0/100 since a Double divided by an Int is a Double, or 20/100.0 for the same reason.

None of these will return 0.20 exactly, since that can't be represented by Double. 20.0/100 is 0.20000000000000001. But I assume this is what you're looking for.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610