From the course: Programming for Non-programmers: iOS 17 and Swift 5
Unlock this course with a free trial
Join today to access over 24,400 courses taught by industry experts.
Formatting the total number
From the course: Programming for Non-programmers: iOS 17 and Swift 5
Formatting the total number
- [Instructor] In our calculator, even though the math works, there's one thing that might stand out to you. If I type 1000 in the calculator, there's no separator between the one and the first zero. We can actually do that using some built-in code in Swift. Let's go over to ButtonGrid and implement that. Find the updateText method toward the bottom of your code, and then above where we set the total and below our if statement, we're going to create something called a number formatter. So type let to create a constant. We'll call this formatter and set it equal to NumberFormatter with a capital N and a capital F. I'm going to choose the one with the parentheses after, just like that. This is a tool that allows you to format a number as a string. On the next line, we're going to set a setting for the formatter called its number style, so type formatter.numberStyle, and set it equal to .decimal. Then we're going to use our formatter to render the total, so replace what's there…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
(Locked)
Storing the saved number5m 38s
-
(Locked)
Showing the total when the equals button is tapped3m 40s
-
(Locked)
Formatting the total number3m 3s
-
(Locked)
Replacing the mode buttons with symbols2m 50s
-
(Locked)
Adding an app icon1m 20s
-
(Locked)
Challenge: Clear button32s
-
(Locked)
Solution: Clear button1m 40s
-
(Locked)
-