Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Robert Ungar
15,723 PointsHow to display thousands separator in Playground?
Hi,
In Swift 2.0 Basics > Swift Types > Type Safety and Type Inference, Prasan has a thousands separator in his yearOfRetirement constant, 2003, so it shows as 2,003. While it doesn’t make sense in context as a year, how can you turn on this option to show it with the thousands separator? I have exactly what he has but mine just shows 2003.
3 Answers
Jhoan Arango
14,575 PointsHello :
Foundation has a class called NSNumberFormatter that will take care of this for you. You can place this code in your playground, and play with it.
import Foundation
// The number we want to play with
let number = 2016
// Creating an instance of NSNumberFormatter
let formatter = NSNumberFormatter()
// Setting the Style we want for this case
formatter.numberStyle = .DecimalStyle
/*
You can also choose .CurrencyStyle, .PercentStyle and others.
Refer to the Side Note below.
*/
// Doing the actual formatting
formatter.stringFromNumber(number) // Prints 2,016
// Side Note : This is the Enum for the format styles you can choose from.
enum NSNumberFormatterStyle : UInt {
case NoStyle
case DecimalStyle
case CurrencyStyle
case PercentStyle
case ScientificStyle
case SpellOutStyle
case OrdinalStyle
case CurrencyISOCodeStyle
case CurrencyPluralStyle
case CurrencyAccountingStyle
}
Hope this helps with your question.
Good luck.
Robert Ungar
15,723 PointsThanks Jhoan Arango and Michael Hulet
Does Prasan have this formatter somewhere else though? Is it maybe just out of the view we're given for the video or in another file? I don't see that in his actual code:
Michael Hulet
47,913 PointsI'm assuming you're using the latest Xcode 7.3, because it does not display the comma by default. I'm not sure exactly when the comma was removed, but I think it's just a matter of the video being recorded with an older version of Xcode
Robert Ungar
15,723 PointsThanks Michael Hulet that's got to be it. I'm using 7.3. It's an odd omission- I didn't see anything in preferences to adjust it so I'll have to search around to see if there's a hidden option or just have to wait for Apple to update
Michael Hulet
47,913 PointsI did a quick search through the settings, and I didn't find one. I wouldn't hold your breath for an update, either. If they removed it, they're probably not putting it back
Michael Hulet
47,913 PointsMichael Hulet
47,913 PointsIt's important to note that
NSNumberFormatteris a class in theFoundationframework, so you'll need toimportit before you can use it, like this:import Foundation