简体   繁体   中英

Swift: Double/Float is not getting decimal values

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM