About Numbers - Images: Japanese
Hey there, everyone!
Next up, let's dig into Numbers in Swift and go over the key things to know.
In the previous article we talked about how strings need to be wrapped in " quotes — but numbers are simpler than that. You don't need any quotes at all; just write the number directly and you're good to go.
Like this:
print(1)
And that gives you:
1
Easy, right?
You can also define negative values. Just like in math, put a - sign in front of the number.
print(-1)
Swift also has a slightly unusual notation that's unique to the language — you can include _ (underscores) inside numbers.
You've probably seen large amounts written with commas every three digits, like "140,000". Swift lets you do the same kind of thing with _ as a separator, wherever you like.
Here's what that looks like in practice:
print(111_999)
Running this gives you:
111999
The _ inside the number is simply ignored, so it works as a visual separator. You might not use this very often, but it's worth knowing about.
In the world of programming, binary (base 2) and hexadecimal (base 16) are frequently used alongside decimal (base 10).
Decimal means numbers that carry over at 10 — in other words, the everyday numbers you use all the time.
Binary uses 0 and 1, carrying over at 2. Hexadecimal uses 0–9 plus A, B, C, D, E, F, carrying over at 16.
In Swift, prefix a number with 0b for binary, or 0x for hexadecimal. Like so:
print(0xFF) // Defines 255 in hexadecimal. print(0b11111111) // Defines 255 in binary.
By the way, the _ separator mentioned earlier works here too. Binary numbers can be pretty hard to read, so using _ to break them up can help a lot.
print(0xF_F) // Underscores work here too. print(0b1111_1111)
Binary and hexadecimal were used heavily in older programming, but as computers have evolved they've become less common in day-to-day code.
That said, there are situations where you genuinely need binary or hexadecimal to get something working, so keep them in the back of your mind.
One more thing worth checking: when you define a plain number, there's a limit to how large it can be.
This applies to other programming languages too, but inside a computer all data is stored as binary — a sequence of 0s and 1s.
The maximum width of a single piece of digital data has to be defined at the hardware design level. Because of this, on a recent Mac running Swift, a plain number defaults to the type Int64 — a 64-bit integer — which means you can only work with values between -9223372036854775808 and 9223372036854775807.
Here's what that means in practice:
print(-9223372036854775808) // This is fine. print(-9223372036854775809) // This is an error. print(9223372036854775807) // This is fine. print(9223372036854775808) // This is an error.
Keep that tucked away in the back of your mind as well.
And that covers numbers in Swift! In the next article, we'll use some basic operators to try out arithmetic. See you there!
This article was written by Sakurama.
Author's beloved small mammal |
桜舞 春人 Sakurama HarutoA Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him. |
If you find any errors or copyright issues, please contact us.