Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any way to add a NSNumberFormatter? #6

Closed
CharlesW95 opened this issue May 17, 2016 · 2 comments
Closed

Is there any way to add a NSNumberFormatter? #6

CharlesW95 opened this issue May 17, 2016 · 2 comments

Comments

@CharlesW95
Copy link

For example, if I would like to display the numbers with commas in between every thousand: 1,000,000, which we can achieve with NSNumberFormatter.

Thanks!

@tbaranes
Copy link
Owner

tbaranes commented May 17, 2016

You can do this using the property stringFormatter which is a closure returning the string formatted, for example:

anIncrementableLabel.stringFormatter = { value in
    let formatter = NumberFormatter()
    formatter.usesGroupingSeparator = true
    formatter.groupingSeparator = ","
    formatter.groupingSize = 3
    return formatter.stringFromNumber(NSNumber(float: value)) ?? ""
}

Also, that will work if you are working with plain string, but you also have attributedTextFormatter which is working the same way but for NSAttributedString

@CharlesW95
Copy link
Author

Ah okay I see. That was very helpful - thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants