From the course: iOS Development Tips

Unlock this course with a free trial

Join today to access over 22,500 courses taught by industry experts.

NumberFormatter

NumberFormatter

- [Instructor] We're all familiar with using string format to format your numbers into strings, but there's a more powerful class to convert your numbers. Let's take a look at the Number Formatter class from Foundation. Now, I've set up a simple playground to work with. Importing Foundation and adding three numbers for testing. Number Formatter uses NSNumbers. So I cast all these numbers to NSNumbers to make this easier to type. Now I'm going to create a formatter named Formatter. So you just do that. Simply saying let formatter = Number Formatter. And it has no parameters. It's just itself. And then we can start using it. And I can use what's known as the string method to format with. So I'm just going to do formatter.string and it takes from an NSNumber here. And I'm going to make that pi. Now this is going to be an optional value. So I'm going to add on the end a case for nil, 'though we don't necessarily need it.…

Contents