Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Besides text, the most common piece of information we work with on a day to day basis is numbers. In programming there are several different ways we can express numeric data and in this video we take a look at the various Swift types  Int, Double and Float  that let us do so.
Further Reading

0:00
There are of course, more types in Swift than just strings.

0:04
In the previous video, we tried to add a number to a string and

0:07
that didn't work, because a number is a totally different type.

0:11
In fact, there are a couple different types.

0:14
Let's take a look.

0:15
Let's add a section marker to our playground to mark off our next

0:20
set of types.

0:21
Now we can use a single line comment, but we have a bunch of comments in here.

0:25
So, it might get lost in a sea of comments.

0:28
Let's jazz it up a bit.

0:30
I'm just going to copy paste this in, so you don't have to watch me type,

0:36
but I've just made a comment across three lines and

0:40
I'm labeling the section integers and

0:43
we can do the same up at the top and I'll mark this as Strings.

0:53
As you might have guessed from the section marker,

0:57
the next type that we're going to work with is Integers.

1:00
Just a tiny refresher from math class,

1:04
an integer is a whole number like 42 or negative 23.

1:08
Now our favorite programming language is Swift,

1:12
so let's start by declaring a string and an assigning a value to it.

1:16
So I'm going to say,

1:20
let favoriteProgrammingLanguage = "Swift".

1:28
We've already worked with integers before when we assigned it to our street address.

1:34
So over here, let's declare a constant called year.

1:38
And to it, we'll assign the year that Swift was created.

1:45
The value, 2014 that we just added is an integer and

1:50
is represented by the type Int in Swift.

1:54
As beginners, that's really all we need to know.

1:58
Now, under the hood, Swift does some extra stuff depending on the size of the number

2:02
we use, but we don't need to worry about that for now.

2:06
But whole numbers aren't the whole picture, pun intended.

2:10
What about partial numbers, like decimal values or fractions?

2:14
Now for that, we have a separate type.

2:16
So I'm going to add another section marker and

2:21
here I'll say, Floating Point Numbers.

2:28
So here's another math refresher.

2:31
Floating Point Numbers are numbers with a fractional component.

2:35
So a number like 3.1415,

2:38
this is a Floating Point Number more commonly known as Pi or

2:43
negative 100.5, anything where we need a decimal place.

2:48
So we have the language, Swift released in 2014.

2:52
When Swift was released, the version number was 1.0.

2:56
But since then, improvements have been made.

2:59
At the time of this recording, we're working with Swift 2.0, a year later.

3:04
So let's record that version number.

3:07
We'll do var version = 2.0.

3:13
My adding this decimal place,

3:15
Swift knows that we're representing a Floating Point Number.

3:19
Even though it just says, 2 here under the hood, we have that 2.0.

3:24
Note that I'm using a variable here instead of a constant,

3:28
because our version number can change.

3:31
In Swift, floating point numbers are represented by two types,

3:36
double and float.

3:38
A double can represent a value having at least 15 decimal digits,

3:44
while float can be as little as 6 decimal digits.

3:48
Now the two different types again have to do with the size of the number and

3:52
Apple's guidance on the matter,

3:54
which we're going to follow is to use double unless you specifically need float.

3:59
So back in our code, let me add a comment with the type specified,

4:03
so it's easier to remember.

4:05
So beside the year, we'll add a comment and say, this type is Int.

4:11
And then beside the version number, we'll say, it's Double.

4:16
Now there is one more type that is a number under the hood, but

4:20
we don't really treat it as such and that is the Boolean type.

4:25
So we'll add another section marker and I'll call this Boolean.

4:33
Now Boolean values are used to model true or false, so

4:38
we can add a constant here to say that Swift is awesome.

4:42
So we'll say, let isAwesome = true and

4:46
we'll add that comment here as well to denote the type.

4:52
In usage, Booleans are represented by true and false, but

4:56
it is actually a binary value.

4:58
Binary means that it is either a one or a zero.

5:02
So the true or false that you see,

5:05
Swift actually reads that as a one for true and a zero for false.

5:10
Now the official type for Boolean values is Bool.

5:15
So we've introduced a couple of new types so far.

5:17
So lets recap everything we've learned.

5:20
First up, we have strings.

5:22
The string type represents text and

5:25
the syntax is text enclosed in two double quotation marks.

5:30
Strings can be manipulated in different ways.

5:34
We can add two strings together, two or

5:37
multiple strings together using concatenation.

5:40
We can use string interpolation to create strings by

5:44
mixing in strings and other types of types.

5:49
Next, we have integers represented by the type Int.

5:53
The Int type represents whole numbers like 50 or 100,

5:58
then we have Floating Point Numbers, represented by either double or float.

6:04
Now a Floating Point Number is a number with a decimal value, like 3.14 or 9.8.

6:11
The choice of whether you use double or float is really best

6:15
left up to Swift to handle for now since it depends on the size of the number.

6:20
We are going to default to using double unless we need to specify the type.

6:26
Finally, we looked at Bool, which represents Boolean values that is true or

6:30
false.

6:32
Under the hood, a one is true and a zero is false.

6:37
This brings our tally of the different types we know up to five.

6:41
Awesome.

6:42
I'll let you finish this video and tackle a code challenge and

6:45
then we'll talk about type safety and type inference.
You need to sign up for Treehouse in order to download course files.
Sign up