Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Enums and Structs Structs and their Methods Struct Methods

What is a double?

What is a double in variables?

4 Answers

There are 3 ways to store numbers: int, double and float.

int - only whole numbers

55
409834
45425346

float - up to 7 decimal places (32-bit)

0.1234567
1234.7654321
22.45

double - up to 15 decimal places (64 bit)

0.123456789123456
22.45
55.0

you do not have to use all the decimal places, it is just imporant to know what type of number will be used and then pick the type with the least amount of decimal places that will hold that number. Example: if you need 9 decimal places, pick double. If you need 6 decimal places, pick float. If you don't need any decimal places then use int.

Piplup Pochama
Piplup Pochama
16,851 Points

Unlike integers (type: Int) which are whole numbers without fractional values, i.e.

1
250
55234125134124

Doubles can be numbers with fractional values, such as

6.234253
8.8
83765438762.357

You can tell when a number is a Double when the number includes a decimal point (also known as a floating point).

Whole numbers can be represented as a Double by adding ".0" at the end:

1.0
250.0
55234125134124.0

Bonus fact: Doubles in Swift are numbers with at least 15 decimal place precision.

jason chan
jason chan
31,009 Points

double is just basically a floating point or basically decimals.

Jake Adams
Jake Adams
1,608 Points

From Apple's documentation:

"Floating-point numbers are numbers with a fractional component, such as 3.14159, 0.1, and -273.15."

A Float is 32-bit floating point number and a Double is a 64-bit floating point number.

Because Float has some accuracy issues, it's always considered best practice to use Double when dealing with currency.