Double vs Float : Swift

Swift has two data types (Double and Float) to hold the decimal values. Both of them hold the same decimal type values but have some differences.

This post was originally posted at https://agrawalsuneet.github.io/blogs/double-vs-float-swift/ and reposted on Medium on 13th April 2022.

The basic difference is around the size of memory they both use based on which their precision varies.

Let’s try to understand the differences between both with example.

Number of Digits

  • The Double type is used to store values in up to 17 places. It starts from the leftmost side and reduces the digits from the right side of the value if exceeding 17 places.
  • The Float type is used to store values in up to 8 places. It also starts from the leftmost side and reduces the digits from the right side of the value if exceeding 8 places.
var doubleVariable : Double = 1234567890.1234567890
var floatVariable : Float = 12345.12345
print(doubleVariable)
//this will print only 17 digits from left
//1234567890.1234567
print(floatVariable)
//this will print only 8 digits from left
//12345.123

Memory Size

  • Double takes 8 bytes of memory.
  • Float takes only 4 bytes of memory
var doubleVariable : Double = 1234567890.1234567890
var floatVariable : Float = 12345.12345
print(MemoryLayout.size(ofValue: doubleVariable))
//this will print
//8 (bytes)
print(MemoryLayout.size(ofValue: floatVariable))
//this will print
//4 (bytes)

Please continue reading at https://agrawalsuneet.github.io/blogs/double-vs-float-swift/

That’s all for now. You can read my other interesting blogs here or you can enjoy my games or apps listed here. Feel free to use my open-source Android components in your app listed here. Or drop an email, if you didn’t find what you are looking for and need some help.

--

--

--

https://agrawalsuneet.github.io/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

LOT & Serial Number.? How it is managed in business and ERP System

Let's start with Python — Simple drawing with Turtle

New Video! Creot Couses — HTML #1

Creating Awesome Login Screen in Flutter

Designing a scalable Notification Service

7+ Key Features of Python that Makes it Different

Guideline to improve test cases on Testing Strategies based on Maintainable Automated Test Suites

User Interaction

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Suneet Agrawal

Suneet Agrawal

https://agrawalsuneet.github.io/

More from Medium

Introduction to concurrency in iOS

Swift: Error Handling (Do, Try, Catch)

Lets coding with together MVVM + RxSwift ( Approach of function in view model )

What is Protocol-Oriented-Programming (POP) in Swift with examples

Protocol-Oriented Programming in Swift