Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Enums and Structs Structs and their Methods Struct Methods

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,008 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.