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

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…

Contents