Swift Float versus Double

Both can be used for Decimal Number Representation. The Float type represents a 32-bit floating-point number, while the Double type represents a 64-bit floating-point number. It is always recommended that we use the Double type rather than the Float type because the Double type has a precision of at least 15 decimal digits whereas the Float type's precision can be as small as six decimal digits.

import UIKit

let myFloat1 : Float = 111.111_111_111
let myFloat2 : Float = 222.222_222_222
print(String(format: "%.9f", myFloat1 + myFloat2))
//Result = 333.333343506

let myDouble1 : Double = 111.111_111_111
let myDouble2 : Double = 222.222_222_222
print(String(format: "%.9f", myDouble1 + myDouble2))
//Result = 333.333333333

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.