NSNumberFormatter Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/Foundation.framework |
| Availability | Available in iOS 2.0 and later. |
| Companion guide | |
| Declared in | NSNumberFormatter.h |
Overview
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.
Many new methods were added to NSNumberFormatter for OS X v10.4 with the intent of making the class interface more like that of CFNumberFormatter, the Core Foundation service on which the class is based. The behavior of an NSNumberFormatter object can conform either to the range of behaviors existing prior to OS X v10.4 or to the range of behavior since that release. (Methods added for and since OS X v10.4 are indicated by a method’s availability statement.) You can determine the current formatter behavior with the formatterBehavior method and you can set the formatter behavior with the setFormatterBehavior: method.
Nomenclature note: NSNumberFormatter provides several methods (such as setMaximumFractionDigits:) that allow you to manage the number of fraction digits allowed as input by an instance: “fraction digits” are the numbers after the decimal separator (in English locales typically referred to as the “decimal point”).
Tasks
Configuring Formatter Behavior and Style
-
– setFormatterBehavior: -
– formatterBehavior -
+ setDefaultFormatterBehavior: -
+ defaultFormatterBehavior -
– setNumberStyle: -
– numberStyle -
– setGeneratesDecimalNumbers: -
– generatesDecimalNumbers
Converting Between Numbers and Strings
-
– getObjectValue:forString:range:error: -
– numberFromString: -
– stringFromNumber: -
+ localizedStringFromNumber:numberStyle:
Managing Localization of Numbers
Configuring Rounding Behavior
Configuring Numeric Formats
-
– setFormatWidth: -
– formatWidth -
– setNegativeFormat: -
– negativeFormat -
– setPositiveFormat: -
– positiveFormat -
– setMultiplier: -
– multiplier
Configuring Numeric Symbols
-
– setPercentSymbol: -
– percentSymbol -
– setPerMillSymbol: -
– perMillSymbol -
– setMinusSign: -
– minusSign -
– setPlusSign: -
– plusSign -
– setExponentSymbol: -
– exponentSymbol -
– setZeroSymbol: -
– zeroSymbol -
– setNilSymbol: -
– nilSymbol -
– setNotANumberSymbol: -
– notANumberSymbol -
– setNegativeInfinitySymbol: -
– negativeInfinitySymbol -
– setPositiveInfinitySymbol: -
– positiveInfinitySymbol
Configuring the Format of Currency
-
– setCurrencySymbol: -
– currencySymbol -
– setCurrencyCode: -
– currencyCode -
– setInternationalCurrencySymbol: -
– internationalCurrencySymbol -
– setCurrencyGroupingSeparator: -
– currencyGroupingSeparator
Configuring Numeric Prefixes and Suffixes
-
– setPositivePrefix: -
– positivePrefix -
– setPositiveSuffix: -
– positiveSuffix -
– setNegativePrefix: -
– negativePrefix -
– setNegativeSuffix: -
– negativeSuffix
Configuring the Display of Numeric Values
-
– setTextAttributesForNegativeValues: -
– textAttributesForNegativeValues -
– setTextAttributesForPositiveValues: -
– textAttributesForPositiveValues -
– setTextAttributesForZero: -
– textAttributesForZero -
– setTextAttributesForNil: -
– textAttributesForNil -
– setTextAttributesForNotANumber: -
– textAttributesForNotANumber -
– setTextAttributesForPositiveInfinity: -
– textAttributesForPositiveInfinity -
– setTextAttributesForNegativeInfinity: -
– textAttributesForNegativeInfinity
Configuring Separators and Grouping Size
-
– setGroupingSeparator: -
– groupingSeparator -
– setUsesGroupingSeparator: -
– usesGroupingSeparator -
– setDecimalSeparator: -
– decimalSeparator -
– setAlwaysShowsDecimalSeparator: -
– alwaysShowsDecimalSeparator -
– setCurrencyDecimalSeparator: -
– currencyDecimalSeparator -
– setGroupingSize: -
– groupingSize -
– setSecondaryGroupingSize: -
– secondaryGroupingSize
Managing the Padding of Numbers
Managing Input and Output Attributes
-
– setAllowsFloats: -
– allowsFloats -
– setMinimum: -
– minimum -
– setMaximum: -
– maximum -
– setMinimumIntegerDigits: -
– minimumIntegerDigits -
– setMinimumFractionDigits: -
– minimumFractionDigits -
– setMaximumIntegerDigits: -
– maximumIntegerDigits -
– setMaximumFractionDigits: -
– maximumFractionDigits
Configuring Significant Digits
-
– setUsesSignificantDigits: -
– usesSignificantDigits -
– setMinimumSignificantDigits: -
– minimumSignificantDigits -
– setMaximumSignificantDigits: -
– maximumSignificantDigits
Managing Leniency Behavior
Managing the Validation of Partial Numeric Strings
Class Methods
defaultFormatterBehavior
Returns an NSNumberFormatterBehavior constant that indicates default formatter behavior for new instances of NSNumberFormatter.
Return Value
An NSNumberFormatterBehavior constant that indicates default formatter behavior for new instances of NSNumberFormatter.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hlocalizedStringFromNumber:numberStyle:
Returns a localized number string with the specified style.
Parameters
- num
The number to localize
- localizationStyle
The localization style to use. See “NSNumberFormatterStyle” for the supported values.
Return Value
An appropriately formatted NSString.
Availability
- Available in iOS 4.0 and later.
Declared In
NSNumberFormatter.hsetDefaultFormatterBehavior:
Sets the default formatter behavior for new instances of NSNumberFormatter .
Parameters
- behavior
An
NSNumberFormatterBehaviorconstant that indicates the revision of the class providing the default behavior.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hInstance Methods
allowsFloats
Returns a Boolean value that indicates whether the receiver allows floating-point values as input.
Return Value
YES if the receiver allows as input floating-point values (that is, values that include the period character [.]), otherwise NO.
Discussion
When this method returns NO, only integer values can be provided as input. The default is YES.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.halwaysShowsDecimalSeparator
Returns a Boolean value that indicates whether the receiver always shows a decimal separator, even if the number is an integer.
Return Value
YES if the receiver always shows a decimal separator, even if the number is an integer, otherwise NO.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hcurrencyCode
Returns the receiver’s currency code as a string.
Return Value
The receiver’s currency code as a string.
Discussion
A currency code is a three-letter code that is, in most cases, composed of a country’s two-character Internet country code plus an extra character to denote the currency unit. For example, the currency code for the Australian dollar is “AUD”. Currency codes are based on the ISO 4217 standard.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hcurrencyDecimalSeparator
Returns the receiver’s currency decimal separator as a string.
Return Value
The receiver’s currency decimal separator as a string.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hcurrencyGroupingSeparator
Returns the currency grouping separator for the receiver.
Return Value
The currency grouping separator for the receiver.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hcurrencySymbol
Returns the receiver’s local currency symbol.
Discussion
A country typically has a local currency symbol and an international currency symbol. The local symbol is used within the country, while the international currency symbol is used in international contexts to specify that country’s currency unambiguously. The local currency symbol is often represented by a Unicode code point.
Availability
- Available in iOS 2.0 and later.
Declared In
NSNumberFormatter.hdecimalSeparator
Returns a string containing the character the receiver uses to represent decimal separators.
Return Value
A string containing the character the receiver uses to represent decimal separators.
Discussion
The return value doesn’t indicate whether decimal separators are enabled.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hexponentSymbol
Returns the string the receiver uses as an exponent symbol.
Return Value
The string the receiver uses as an exponent symbol.
Discussion
The exponent symbol is the “E” or “e” in the scientific notation of numbers, as in 1.0e+56.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hformatterBehavior
Returns an NSNumberFormatterBehavior constant that indicates the formatter behavior of the receiver.
Return Value
An NSNumberFormatterBehavior constant that indicates the formatter behavior of the receiver.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hformatWidth
Returns the format width of the receiver.
Discussion
The format width is the number of characters of a formatted number within a string that is either left justified or right justified based on the value returned from paddingPosition.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hgeneratesDecimalNumbers
Returns a Boolean value that indicates whether the receiver creates instances of NSDecimalNumber when it converts strings to number objects.
Return Value
YES if the receiver creates instances of NSDecimalNumber when it converts strings to number objects, NO if it creates instance of NSNumber.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hgetObjectValue:forString:range:error:
Returns by reference a cell-content object after creating it from a range of characters in a given string.
Parameters
- anObject
On return, contains an instance of
NSDecimalNumberorNSNumberbased on the current value ofgeneratesDecimalNumbers. The default is to returnNSDecimalNumberinstances- aString
A string object with the range of characters specified in rangep that is used to create anObject.
- rangep
A range of characters in aString. On return, contains the actual range of characters used to create the object.
- error
If an error occurs, upon return contains an
NSErrorobject that explains the reason why the conversion failed. If you pass innilfor error you are indicating that you are not interested in error information.
Return Value
YES if the conversion from string to cell-content object was successful, otherwise NO.
Discussion
If there is an error, the delegate (if any) of the control object managing the cell can then respond to the failure in the NSControl delegation method control:didFailToFormatString:errorDescription:.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hgroupingSeparator
Returns a string containing the receiver’s grouping separator.
Return Value
A string containing the receiver’s grouping separator.
Discussion
For example, the grouping separator used in the United States is the comma (“10,000”) whereas in France it is the space (“10 000”).
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hgroupingSize
Returns the receiver’s primary grouping size.
Return Value
The receiver’s primary grouping size.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hinternationalCurrencySymbol
Returns the international currency symbol used by the receiver.
Discussion
A country typically has a local currency symbol and an international currency symbol. The local symbol is used within the country, while the international currency symbol is used in international contexts to specify that country’s currency unambiguously. The international currency symbol is often represented by a Unicode code point.
Availability
- Available in iOS 2.0 and later.
Declared In
NSNumberFormatter.hisLenient
Returns a Boolean value that indicates whether the receiver uses heuristics to guess at the number which is intended by a string.
Return Value
YES if the receiver uses heuristics to guess at the number which is intended by the string; otherwise NO.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hisPartialStringValidationEnabled
Returns a Boolean value that indicates whether partial string validation is enabled.
Return Value
YES if partial string validation is enabled, otherwise NO.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hlocale
Returns the locale of the receiver.
Return Value
The locale of the receiver.
Discussion
A number formatter’s locale specifies default localization attributes, such as ISO country and language codes, currency code, calendar, system of measurement, and decimal separator.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hmaximum
Returns the highest number allowed as input by the receiver.
Return Value
The highest number allowed as input by the receiver or nil, meaning no limit.
Discussion
For versions prior to OS X v10.4 (and number-formatter behavior set to NSNumberFormatterBehavior10_0) this method returns an NSDecimalNumber object.
Availability
- Available in iOS 2.0 and later.
Declared In
NSNumberFormatter.hmaximumFractionDigits
Returns the maximum number of digits after the decimal separator allowed as input and output by the receiver.
Return Value
The maximum number of digits after the decimal separator allowed as input and output.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hmaximumIntegerDigits
Returns the maximum number of integer digits allowed as input and output by the receiver.
Return Value
The maximum number of integer digits allowed as input and output.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hmaximumSignificantDigits
Returns the maximum number of significant digits for the receiver.
Return Value
The maximum number of significant digits for the receiver.
Availability
- Available in iOS 2.0 and later.
Declared In
NSNumberFormatter.hminimum
Returns the lowest number allowed as input by the receiver.
Return Value
The lowest number allowed as input by the receiver or nil, meaning no limit.
Discussion
For versions prior to OS X v10.4 (and number-formatter behavior set to NSNumberFormatterBehavior10_0) this method returns an NSDecimalNumber object.
Availability
- Available in iOS 2.0 and later.
Declared In
NSNumberFormatter.hminimumFractionDigits
Returns the minimum number of digits after the decimal separator allowed as input and output by the receiver.
Return Value
The minimum number of digits after the decimal separator allowed as input and output.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hminimumIntegerDigits
Returns the minimum number of integer digits allowed as input and output by the receiver.
Return Value
The minimum number of integer digits allowed as input and output.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hminimumSignificantDigits
Returns the minimum number of significant digits for the receiver.
Return Value
The minimum number of significant digits for the receiver.
Availability
- Available in iOS 2.0 and later.
Declared In
NSNumberFormatter.hminusSign
Returns the string the receiver uses to represent the minus sign.
Return Value
The string that represents the receiver’s minus sign.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hmultiplier
Returns the multiplier used by the receiver as an NSNumber object.
Discussion
A multiplier is a factor used in conversions between numbers and strings (that is, numbers as stored and numbers as displayed). When the input value is a string, the multiplier is used to divide, and when the input value is a number, the multiplier is used to multiply. These operations allow the formatted values to be different from the values that a program manipulates internally.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hnegativeFormat
Returns the format used by the receiver to display negative numbers.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hnegativeInfinitySymbol
Returns the symbol the receiver uses to represent negative infinity.
Return Value
The symbol the receiver uses to represent negative infinity.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hnegativePrefix
Returns the string the receiver inserts as a prefix to negative values.
Return Value
The string the receiver inserts as a prefix to negative values.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hnegativeSuffix
Returns the string the receiver adds as a suffix to negative values.
Return Value
The string the receiver adds as a suffix to negative values.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hnilSymbol
Returns the string the receiver uses to represent a nil value.
Return Value
The string the receiver uses to represent a nil value.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hnotANumberSymbol
Returns the symbol the receiver uses to represent NaN (“not a number”) when it converts values.
Return Value
The symbol the receiver uses to represent NaN (“not a number”) when it converts values.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hnumberFromString:
Returns an NSNumber object created by parsing a given string.
Parameters
- string
An
NSStringobject that is parsed to generate the returned number object.
Return Value
An NSNumber object created by parsing string using the receiver’s format.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hnumberStyle
Returns the number-formatter style of the receiver.
Return Value
An NSNumberFormatterStyle constant that indicates the number-formatter style of the receiver.
Discussion
Styles are essentially predetermined sets of values for certain properties. Examples of number-formatter styles are those used for decimal values, percentage values, and currency.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hpaddingCharacter
Returns a string containing the padding character for the receiver.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hpaddingPosition
Returns the padding position of the receiver.
Discussion
The returned constant indicates whether the padding is before or after the number’s prefix or suffix.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hpercentSymbol
Returns the string that the receiver uses to represent the percent symbol.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hperMillSymbol
Returns the string that the receiver uses for the per-thousands symbol.
Return Value
The string that the receiver uses for the per-thousands symbol.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hplusSign
Returns the string the receiver uses for the plus sign.
Return Value
The string the receiver uses for the plus sign.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hpositiveFormat
Returns the format used by the receiver to display positive numbers.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hpositiveInfinitySymbol
Returns the string the receiver uses for the positive infinity symbol.
Return Value
The string the receiver uses for the positive infinity symbol.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hpositivePrefix
Returns the string the receiver uses as the prefix for positive values.
Return Value
The string the receiver uses as the prefix for positive values.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hpositiveSuffix
Returns the string the receiver uses as the suffix for positive values.
Return Value
The string the receiver uses as the suffix for positive values.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hroundingIncrement
Returns the rounding increment used by the receiver.
Return Value
The rounding increment used by the receiver.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hroundingMode
Returns the rounding mode used by the receiver.
Return Value
The rounding mode used by the receiver.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsecondaryGroupingSize
Returns the size of secondary groupings for the receiver.
Return Value
The size of secondary groupings for the receiver.
Discussion
Some locales allow the specification of another grouping size for larger numbers. For example, some locales may represent a number such as 61, 242, 378.46 (as in the United States) as 6,12,42,378.46. In this case, the secondary grouping size (covering the groups of digits furthest from the decimal point) is 2.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetAllowsFloats:
Sets whether the receiver allows as input floating-point values (that is, values that include the period character [.]).
Parameters
- flag
YESif the receiver allows floating-point values,NOotherwise.
Discussion
By default, floating point values are allowed as input.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetAlwaysShowsDecimalSeparator:
Controls whether the receiver always shows the decimal separator, even for integer numbers.
Parameters
- flag
YESif the receiver should always show the decimal separator,NOotherwise.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetCurrencyCode:
Sets the receiver’s currency code.
Parameters
- string
A string specifying the receiver's new currency code.
Discussion
A currency code is a three-letter code that is, in most cases, composed of a country’s two-character Internet country code plus an extra character to denote the currency unit. For example, the currency code for the Australian dollar is “AUD”. Currency codes are based on the ISO 4217 standard.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetCurrencyDecimalSeparator:
Sets the string used by the receiver as a decimal separator.
Parameters
- string
The string to use as the currency decimal separator.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetCurrencyGroupingSeparator:
Sets the currency grouping separator for the receiver.
Parameters
- string
The currency grouping separator for the receiver.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetCurrencySymbol:
Sets the string used by the receiver as a local currency symbol.
Parameters
- string
A string that represents a local currency symbol.
Discussion
The local symbol is used within the country, while the international currency symbol is used in international contexts to specify that country’s currency unambiguously. The local currency symbol is often represented by a Unicode code point.
Availability
- Available in iOS 2.0 and later.
Declared In
NSNumberFormatter.hsetDecimalSeparator:
Sets the character the receiver uses as a decimal separator.
Parameters
- newSeparator
The string that specifies the decimal-separator character to use. If newSeparator contains multiple characters, only the first one is used.
Discussion
If you don’t have decimal separators enabled through another means (such as setFormat:), using this method enables them.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetExponentSymbol:
Sets the string used by the receiver to represent the exponent symbol.
Parameters
- string
A string that represents an exponent symbol.
Discussion
The exponent symbol is the “E” or “e” in the scientific notation of numbers, as in 1.0e+56.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetFormatterBehavior:
Sets the formatter behavior of the receiver.
Parameters
- behavior
An
NSNumberFormatterBehaviorconstant that indicates the revision of theNSNumberFormatterclass providing the current behavior.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetFormatWidth:
Sets the format width used by the receiver.
Parameters
- number
An integer that specifies the format width.
Discussion
The format width is the number of characters of a formatted number within a string that is either left justified or right justified based on the value returned from paddingPosition.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetGeneratesDecimalNumbers:
Controls whether the receiver creates instances of NSDecimalNumber when it converts strings to number objects.
Parameters
- flag
YESif the receiver should generateNSDecimalNumberinstances,NOif it should generateNSNumberinstances.
Discussion
The default is YES.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetGroupingSeparator:
Specifies the string used by the receiver for a grouping separator.
Parameters
- string
A string that specifies the grouping separator to use.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetGroupingSize:
Sets the grouping size of the receiver.
Parameters
- numDigits
An integer that specifies the grouping size.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetInternationalCurrencySymbol:
Sets the string used by the receiver for the international currency symbol.
Parameters
- string
A string that represents an international currency symbol.
Discussion
The local symbol is used within the country, while the international currency symbol is used in international contexts to specify that country’s currency unambiguously. The local currency symbol is often represented by a Unicode code point.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetLenient:
Sets whether the receiver will use heuristics to guess at the number which is intended by a string.
Parameters
- b
YESif the receiver will use heuristics to guess at the number which is intended by the string; otherwiseNO.
Discussion
If the formatter is set to be lenient, as with any guessing it may get the result number wrong (that is, a number other than that which was intended).
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetLocale:
Sets the locale of the receiver.
Parameters
- theLocale
An
NSLocaleobject representing the new locale of the receiver.
Discussion
The locale determines the default values for many formatter attributes, such as ISO country and language codes, currency code, calendar, system of measurement, and decimal separator.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetMaximum:
Sets the highest number the receiver allows as input.
Parameters
- aMaximum
A number object that specifies a maximum input value.
Discussion
If aMaximum is nil, checking for the maximum value is disabled. For versions prior to OS X v10.4 (and number-formatter behavior set to NSNumberFormatterBehavior10_0) this method requires an NSDecimalNumber argument.
Availability
- Available in iOS 2.0 and later.
Declared In
NSNumberFormatter.hsetMaximumFractionDigits:
Sets the maximum number of digits after the decimal separator allowed as input and output by the receiver.
Parameters
- number
The maximum number of digits after the decimal separator allowed as input and output.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetMaximumIntegerDigits:
Sets the maximum number of integer digits allowed as input and output by the receiver.
Parameters
- number
The maximum number of integer digits allowed as input and output.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetMaximumSignificantDigits:
Sets the maximum number of significant digits for the receiver.
Parameters
- number
The maximum number of significant digits for the receiver.
Availability
- Available in iOS 2.0 and later.
Declared In
NSNumberFormatter.hsetMinimum:
Sets the lowest number the receiver allows as input.
Parameters
- aMinimum
A number object that specifies a minimum input value.
Discussion
If aMinimum is nil, checking for the minimum value is disabled. For versions prior to OS X v10.4 (and number-formatter behavior set to NSNumberFormatterBehavior10_0) this method requires an NSDecimalNumber argument.
Availability
- Available in iOS 2.0 and later.
Declared In
NSNumberFormatter.hsetMinimumFractionDigits:
Sets the minimum number of digits after the decimal separator allowed as input and output by the receiver.
Parameters
- number
The minimum number of digits after the decimal separator allowed as input and output.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetMinimumIntegerDigits:
Sets the minimum number of integer digits allowed as input and output by the receiver.
Parameters
- number
The minimum number of integer digits allowed as input and output.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetMinimumSignificantDigits:
Sets the minimum number of significant digits for the receiver.
Parameters
- number
The minimum number of significant digits for the receiver.
Availability
- Available in iOS 2.0 and later.
Declared In
NSNumberFormatter.hsetMinusSign:
Sets the string used by the receiver for the minus sign.
Parameters
- string
A string that represents a minus sign.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetMultiplier:
Sets the multiplier of the receiver.
Parameters
- number
A number object that represents a multiplier.
Discussion
A multiplier is a factor used in conversions between numbers and strings (that is, numbers as stored and numbers as displayed). When the input value is a string, the multiplier is used to divide, and when the input value is a number, the multiplier is used to multiply. These operations allow the formatted values to be different from the values that a program manipulates internally.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetNegativeFormat:
Sets the format the receiver uses to display negative values.
Parameters
- aFormat
A string that specifies the format for negative values.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetNegativeInfinitySymbol:
Sets the string used by the receiver for the negative infinity symbol.
Parameters
- string
A string that represents a negative infinity symbol.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetNegativePrefix:
Sets the string the receiver uses as a prefix for negative values.
Parameters
- string
A string to use as the prefix for negative values.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetNegativeSuffix:
Sets the string the receiver uses as a suffix for negative values.
Parameters
- string
A string to use as the suffix for negative values.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetNilSymbol:
Sets the string the receiver uses to represent nil values.
Parameters
- string
A string that represents a
nilvalue.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetNotANumberSymbol:
Sets the string the receiver uses to represent NaN (“not a number”).
Parameters
- string
A string that represents a NaN symbol.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetNumberStyle:
Sets the number style used by the receiver.
Parameters
- style
An
NSNumberFormatterStyleconstant that specifies a formatter style.
Discussion
Styles are essentially predetermined sets of values for certain properties. Examples of number-formatter styles are those used for decimal values, percentage values, and currency.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetPaddingCharacter:
Sets the string that the receiver uses to pad numbers in the formatted string representation.
Parameters
- string
A string containing a padding character (or characters).
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetPaddingPosition:
Sets the padding position used by the receiver.
Parameters
- position
An
NSNumberFormatterPadPositionconstant that indicates a padding position (before or after prefix or suffix).
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetPartialStringValidationEnabled:
Sets whether partial string validation is enabled for the receiver.
Parameters
- enabled
YESif partial string validation is enabled, otherwiseNO.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetPercentSymbol:
Sets the string used by the receiver to represent the percent symbol.
Parameters
- string
A string that represents a percent symbol.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetPerMillSymbol:
Sets the string used by the receiver to represent the per-mill (per-thousand) symbol.
Parameters
- string
A string that represents a per-mill symbol.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetPlusSign:
Sets the string used by the receiver to represent the plus sign.
Parameters
- string
A string that represents a plus sign.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetPositiveFormat:
Sets the format the receiver uses to display positive values.
Parameters
- aFormat
A string that specifies the format for positive values.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetPositiveInfinitySymbol:
Sets the string used by the receiver for the positive infinity symbol.
Parameters
- string
A string that represents a positive infinity symbol.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetPositivePrefix:
Sets the string the receiver uses as the prefix for positive values.
Parameters
- string
A string to use as the prefix for positive values.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetPositiveSuffix:
Sets the string the receiver uses as the suffix for positive values.
Parameters
- string
A string to use as the suffix for positive values.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetRoundingIncrement:
Sets the rounding increment used by the receiver.
Parameters
- number
A number object specifying a rounding increment.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetRoundingMode:
Sets the rounding mode used by the receiver.
Parameters
- mode
An
NSNumberFormatterRoundingModeconstant that indicates a rounding mode.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetSecondaryGroupingSize:
Sets the secondary grouping size of the receiver.
Parameters
- number
An integer that specifies the size of secondary groupings.
Discussion
Some locales allow the specification of another grouping size for larger numbers. For example, some locales may represent a number such as 61, 242, 378.46 (as in the United States) as 6,12,42,378.46. In this case, the secondary grouping size (covering the groups of digits furthest from the decimal point) is 2.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetTextAttributesForNegativeInfinity:
Sets the text attributes used to display the negative infinity symbol.
Parameters
- newAttributes
A dictionary containing text attributes for the display of the negative infinity symbol.
Availability
- Available in iOS 2.0 and later.
Declared In
NSNumberFormatter.hsetTextAttributesForNegativeValues:
Sets the text attributes to be used in displaying negative values .
Parameters
- newAttributes
A dictionary containing properties for the display of negative values.
Discussion
For example, this code excerpt causes negative values to be displayed in red:
NSNumberFormatter *numberFormatter = |
[[[NSNumberFormatter alloc] init] autorelease]; |
NSMutableDictionary *newAttrs = [NSMutableDictionary dictionary]; |
[numberFormatter setFormat:@"$#,##0.00;($#,##0.00)"]; |
[newAttrs setObject:[NSColor redColor] forKey:@"NSColor"]; |
[numberFormatter setTextAttributesForNegativeValues:newAttrs]; |
[[textField cell] setFormatter:numberFormatter]; |
An even simpler way to cause negative values to be displayed in red is to include the constant [Red] in your format string, as shown in this example:
[numberFormatter setFormat:@"$#,##0.00;[Red]($#,##0.00)"]; |
When you set a value’s text attributes to use color, the color appears only when the value’s cell doesn’t have input focus. When the cell has input focus, the value is displayed in standard black.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetTextAttributesForNil:
Sets the text attributes used to display the nil symbol.
Parameters
- newAttributes
A dictionary containing text attributes for the display of the
nilsymbol.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetTextAttributesForNotANumber:
Sets the text attributes used to display the NaN ("not a number") string.
Parameters
- newAttributes
A dictionary containing text attributes for the display of the NaN symbol.
Availability
- Available in iOS 2.0 and later.
Declared In
NSNumberFormatter.hsetTextAttributesForPositiveInfinity:
Sets the text attributes used to display the positive infinity symbol.
Parameters
- newAttributes
A dictionary containing text attributes for the display of the positive infinity symbol.
Availability
- Available in iOS 2.0 and later.
Declared In
NSNumberFormatter.hsetTextAttributesForPositiveValues:
Sets the text attributes to be used in displaying positive values.
Parameters
- newAttributes
A dictionary containing text attributes for the display of positive values.
Discussion
See setTextAttributesForNegativeValues: for an example of how a related method might be used.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetTextAttributesForZero:
Sets the text attributes used to display a zero value.
Parameters
- newAttributes
A dictionary containing text attributes for the display of zero values.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetUsesGroupingSeparator:
Controls whether the receiver displays the grouping separator.
Parameters
- flag
YESif the receiver should display the grouping separator,NOotherwise.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hsetUsesSignificantDigits:
Sets whether the receiver uses significant digits.
Parameters
- b
YESif the receiver uses significant digits, otherwiseNO.
Availability
- Available in iOS 2.0 and later.
Declared In
NSNumberFormatter.hsetZeroSymbol:
Sets the string the receiver uses as the symbol to show the value zero.
Parameters
- string
The string the receiver uses as the symbol to show the value zero.
Discussion
By default this is 0; you might want to set it to, for example, “ - ”, similar to the way that a spreadsheet might when a column is defined as accounting.
Special Considerations
On OS X v10.4, this method works correctly for 10_0-style number formatters but does not work correctly for 10_4-style number formatters. You can work around the problem by subclassing and overriding the methods that convert between strings and numbers to look for the zero cases first and provide different behavior, invoking super when not zero.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hstringFromNumber:
Returns a string containing the formatted value of the provided number object.
Parameters
- number
An
NSNumberobject that is parsed to create the returned string object.
Return Value
A string containing the formatted value of number using the receiver’s current settings.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.htextAttributesForNegativeInfinity
Returns a dictionary containing the text attributes used to display the negative infinity string.
Return Value
A dictionary containing the text attributes used to display the negative infinity string.
Availability
- Available in iOS 2.0 and later.
Declared In
NSNumberFormatter.htextAttributesForNegativeValues
Returns a dictionary containing the text attributes that have been set for negative values.
Return Value
A dictionary containing the text attributes that have been set for negative values.
Availability
- Available in iOS 2.0 and later.
Declared In
NSNumberFormatter.htextAttributesForNil
Returns a dictionary containing the text attributes used to display the nil symbol.
Return Value
A dictionary containing the text attributes used to display the nil symbol.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.htextAttributesForNotANumber
Returns a dictionary containing the text attributes used to display the NaN ("not a number") symbol.
Return Value
A dictionary containing the text attributes used to display the NaN ("not a number") symbol.
Availability
- Available in iOS 2.0 and later.
Declared In
NSNumberFormatter.htextAttributesForPositiveInfinity
Returns a dictionary containing the text attributes used to display the positive infinity symbol.
Return Value
A dictionary containing the text attributes used to display the positive infinity symbol.
Availability
- Available in iOS 2.0 and later.
Declared In
NSNumberFormatter.htextAttributesForPositiveValues
Returns a dictionary containing the text attributes that have been set for positive values.
Return Value
A dictionary containing the text attributes that have been set for positive values.
Availability
- Available in iOS 2.0 and later.
Declared In
NSNumberFormatter.htextAttributesForZero
Returns a dictionary containing the text attributes used to display a value of zero.
Return Value
A dictionary containing the text attributes used to display a value of zero.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.husesGroupingSeparator
Returns a Boolean value that indicates whether the receiver uses the grouping separator.
Return Value
YES if the receiver uses the grouping separator, otherwise NO.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.husesSignificantDigits
Returns a Boolean value that indicates whether the receiver uses significant digits.
Return Value
YES if the receiver uses significant digits, otherwise NO.
Availability
- Available in iOS 2.0 and later.
Declared In
NSNumberFormatter.hzeroSymbol
Returns the string the receiver uses as the symbol to show the value zero.
Return Value
The string the receiver uses as the symbol to show the value zero.
Discussion
For a discussion of how this is used, see setZeroSymbol:.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSNumberFormatter.hConstants
NSNumberFormatterStyle
These constants specify predefined number format styles. These constants are used by the numberStyle and setNumberStyle: methods.
enum {
NSNumberFormatterNoStyle = kCFNumberFormatterNoStyle,
NSNumberFormatterDecimalStyle = kCFNumberFormatterDecimalStyle,
NSNumberFormatterCurrencyStyle = kCFNumberFormatterCurrencyStyle,
NSNumberFormatterPercentStyle = kCFNumberFormatterPercentStyle,
NSNumberFormatterScientificStyle = kCFNumberFormatterScientificStyle,
NSNumberFormatterSpellOutStyle = kCFNumberFormatterSpellOutStyle
};
typedef NSUInteger NSNumberFormatterStyle;
Constants
NSNumberFormatterNoStyleSpecifies no style.
Available in iOS 2.0 and later.
Declared in
NSNumberFormatter.h.NSNumberFormatterDecimalStyleSpecifies a decimal style format.
Available in iOS 2.0 and later.
Declared in
NSNumberFormatter.h.NSNumberFormatterCurrencyStyleSpecifies a currency style format.
Available in iOS 2.0 and later.
Declared in
NSNumberFormatter.h.NSNumberFormatterPercentStyleSpecifies a percent style format.
Available in iOS 2.0 and later.
Declared in
NSNumberFormatter.h.NSNumberFormatterScientificStyleSpecifies a scientific style format.
Available in iOS 2.0 and later.
Declared in
NSNumberFormatter.h.NSNumberFormatterSpellOutStyleSpecifies a spell-out format; for example, “23” becomes “twenty-three”.
Available in iOS 2.0 and later.
Declared in
NSNumberFormatter.h.
NSNumberFormatterBehavior
These constants specify the behavior of a number formatter. These constants are returned by the defaultFormatterBehavior class method and the formatterBehavior instance methods; you set them with the setDefaultFormatterBehavior: class method and the setFormatterBehavior: instance method.
enum {
NSNumberFormatterBehaviorDefault = 0,
NSNumberFormatterBehavior10_0 = 1000,
NSNumberFormatterBehavior10_4 = 1040,
};
typedef NSUInteger NSNumberFormatterBehavior;
Constants
NSNumberFormatterBehaviorDefaultThe number-formatter behavior set as the default for new instances. You can set the default formatter behavior with the class method
setDefaultFormatterBehavior:.Available in iOS 2.0 and later.
Declared in
NSNumberFormatter.h.NSNumberFormatterBehavior10_0The number-formatter behavior as it existed prior to OS X v10.4.
Available in iOS 2.0 through iOS 2.1.
Declared in
NSNumberFormatter.h.NSNumberFormatterBehavior10_4The number-formatter behavior since OS X v10.4.
Available in iOS 2.0 and later.
Declared in
NSNumberFormatter.h.
NSNumberFormatterPadPosition
These constants are used to specify how numbers should be padded. These constants are used by the paddingPosition and setPaddingPosition: methods.
enum {
NSNumberFormatterPadBeforePrefix = kCFNumberFormatterPadBeforePrefix,
NSNumberFormatterPadAfterPrefix = kCFNumberFormatterPadAfterPrefix,
NSNumberFormatterPadBeforeSuffix = kCFNumberFormatterPadBeforeSuffix,
NSNumberFormatterPadAfterSuffix = kCFNumberFormatterPadAfterSuffix
};
typedef NSUInteger NSNumberFormatterPadPosition;
Constants
NSNumberFormatterPadBeforePrefixSpecifies that the padding should occur before the prefix.
Available in iOS 2.0 and later.
Declared in
NSNumberFormatter.h.NSNumberFormatterPadAfterPrefixSpecifies that the padding should occur after the prefix.
Available in iOS 2.0 and later.
Declared in
NSNumberFormatter.h.NSNumberFormatterPadBeforeSuffixSpecifies that the padding should occur before the suffix.
Available in iOS 2.0 and later.
Declared in
NSNumberFormatter.h.NSNumberFormatterPadAfterSuffixSpecifies that the padding should occur after the suffix.
Available in iOS 2.0 and later.
Declared in
NSNumberFormatter.h.
NSNumberFormatterRoundingMode
These constants are used to specify how numbers should be rounded. These constants are used by the roundingModeand setRoundingMode: methods.
enum {
NSNumberFormatterRoundCeiling = kCFNumberFormatterRoundCeiling,
NSNumberFormatterRoundFloor = kCFNumberFormatterRoundFloor,
NSNumberFormatterRoundDown = kCFNumberFormatterRoundDown,
NSNumberFormatterRoundUp = kCFNumberFormatterRoundUp,
NSNumberFormatterRoundHalfEven = kCFNumberFormatterRoundHalfEven,
NSNumberFormatterRoundHalfDown = kCFNumberFormatterRoundHalfDown,
NSNumberFormatterRoundHalfUp = kCFNumberFormatterRoundHalfUp
};
typedef NSUInteger NSNumberFormatterRoundingMode;
Constants
NSNumberFormatterRoundCeilingRound up to next larger number with the proper number of digits after the decimal separator.
Available in iOS 2.0 and later.
Declared in
NSNumberFormatter.h.NSNumberFormatterRoundFloorRound down to next smaller number with the proper number of digits after the decimal separator.
Available in iOS 2.0 and later.
Declared in
NSNumberFormatter.h.NSNumberFormatterRoundDownRound down to next smaller number with the proper number of digits after the decimal separator.
Available in iOS 2.0 and later.
Declared in
NSNumberFormatter.h.NSNumberFormatterRoundHalfEvenRound the last digit, when followed by a 5, toward an even digit (.25 -> .2, .35 -> .4)
Available in iOS 2.0 and later.
Declared in
NSNumberFormatter.h.NSNumberFormatterRoundUpRound up to next larger number with the proper number of digits after the decimal separator.
Available in iOS 2.0 and later.
Declared in
NSNumberFormatter.h.NSNumberFormatterRoundHalfDownRound down when a 5 follows putative last digit.
Available in iOS 2.0 and later.
Declared in
NSNumberFormatter.h.NSNumberFormatterRoundHalfUpRound up when a 5 follows putative last digit.
Available in iOS 2.0 and later.
Declared in
NSNumberFormatter.h.
© 2013 Apple Inc. All Rights Reserved. (Last updated: 2013-04-23)