0
0
Swiftprogramming~15 mins

Numeric literal formats in Swift - Mini Project: Build & Apply

Choose your learning style9 modes available
Numeric Literal Formats in Swift
📖 Scenario: You are working on a simple calculator app that needs to handle different types of numbers. To do this, you must understand how to write numbers in Swift using different numeric literal formats.
🎯 Goal: Learn how to write and use different numeric literal formats in Swift, including decimal, binary, octal, and hexadecimal numbers.
📋 What You'll Learn
Create variables using decimal, binary, octal, and hexadecimal numeric literals
Use underscores to improve readability in large numbers
Print the values of these variables to see their decimal equivalents
💡 Why This Matters
🌍 Real World
Understanding numeric literals helps when working with low-level data, colors in hex, or binary flags in real apps.
💼 Career
Many programming jobs require reading and writing numbers in different formats, especially in systems programming, embedded devices, or graphics.
Progress0 / 4 steps
1
Create decimal and binary numeric literals
Create a variable called decimalNumber and set it to the decimal number 42. Then create a variable called binaryNumber and set it to the binary number 0b101010.
Swift
Need a hint?

Use var to create variables. Binary numbers start with 0b.

2
Add octal and hexadecimal numeric literals
Add a variable called octalNumber and set it to the octal number 0o52. Also add a variable called hexNumber and set it to the hexadecimal number 0x2A.
Swift
Need a hint?

Octal numbers start with 0o and hexadecimal numbers start with 0x.

3
Use underscores for readability
Create a variable called largeNumber and set it to the decimal number 1_000_000 using underscores to separate the thousands.
Swift
Need a hint?

Use underscores _ inside numbers to make them easier to read.

4
Print all numeric variables
Print the values of decimalNumber, binaryNumber, octalNumber, hexNumber, and largeNumber each on a new line.
Swift
Need a hint?

Use print() to display each variable's value on its own line.