Instances of NSNumberFormatter format the textual representation of cells that contain NSNumber objects and convert textual representations of numeric values into NSNumber objects. The representation encompasses integers, floats, and doubles; floats and doubles can be formatted to a specified decimal position. NSNumberFormatter objects can also impose ranges on the numeric values cells can accept.
Language
- Swift
- Objective-C
SDKs
- iOS 8.0+
- macOS 10.10+
- tvOS 9.0+
- watchOS 2.0+
Overview
Fraction Digits and Significant Digits
The NSNumberFormatter class provides flexible options for displaying non-zero fractional parts of numbers.
If you set the usesSignificantDigits property to true, you can configure NSNumberFormatter to display significant digits using the minimumSignificantDigits and maximumSignificantDigits properties. If usesSignificantDigits is false, these properties are ignored.
Otherwise, you can configure the minimum and maximum number of integer and fraction digits, or the numbers before and after the decimal separator, respectively, using the minimumIntegerDigits, maximumIntegerDigits, minimumFractionDigits, and maximumFractionDigits properties.
Thread Safety
On iOS 7 and later NSNumberFormatter is thread safe.
In macOS 10.9 and later NSNumberFormatter is thread safe so long as you are using the modern behavior in a 64-bit app.
On earlier versions of the operating system, or when using the legacy formatter behavior or running in 32-bit in macOS, NSNumberFormatter is not thread safe, and you therefore must not mutate a number formatter simultaneously from multiple threads.