Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/Foundation.framework |
Availability | Available in Mac OS X v10.0 and later. |
Companion guide | |
Declared in | NSNumberFormatter.h |
Related sample code |
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 Mac 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 Mac OS X v10.4 or to the range of behavior since that release. (Methods added for and since Mac 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.
iPhone OS Note: iPhone OS supports only the modern 10.4+ behavior. 10.0-style methods and format strings are not available on iPhone OS.
Important:
The pre-Mac OS X v10.4 methods of NSNumberFormatter
are not compatible with the methods added for Mac OS X v10.4. An NSNumberFormatter
object should not invoke methods in these different behavior groups indiscriminately. Use the old-style methods if you have configured the number-formatter behavior to be NSNumberFormatterBehavior10_0
. Use the new methods instead of the older-style ones if you have configured the number-formatter behavior to be NSNumberFormatterBehavior10_4
.
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”).
– setFormatterBehavior:
– formatterBehavior
+ setDefaultFormatterBehavior:
+ defaultFormatterBehavior
– setNumberStyle:
– numberStyle
– setGeneratesDecimalNumbers:
– generatesDecimalNumbers
– setRoundingBehavior:
– roundingBehavior
– setRoundingIncrement:
– roundingIncrement
– setRoundingMode:
– roundingMode
– setFormat:
– formatWidth
– setNegativeFormat:
– negativeFormat
– setPositiveFormat:
– positiveFormat
– setFormatWidth:
– format
– setMultiplier:
– multiplier
– percentSymbol
– setPercentSymbol:
– perMillSymbol
– setPerMillSymbol:
– minusSign
– setMinusSign:
– plusSign
– setPlusSign:
– exponentSymbol
– setExponentSymbol:
– zeroSymbol
– setZeroSymbol:
– nilSymbol
– setNilSymbol:
– notANumberSymbol
– setNotANumberSymbol:
– negativeInfinitySymbol
– setNegativeInfinitySymbol:
– positiveInfinitySymbol
– setPositiveInfinitySymbol:
– setCurrencySymbol:
– currencySymbol
– setCurrencyCode:
– currencyCode
– setInternationalCurrencySymbol:
– internationalCurrencySymbol
– setCurrencyGroupingSeparator:
– currencyGroupingSeparator
– setPositivePrefix:
– positivePrefix
– setPositiveSuffix:
– positiveSuffix
– setNegativePrefix:
– negativePrefix
– setNegativeSuffix:
– negativeSuffix
– setTextAttributesForNegativeValues:
– textAttributesForNegativeValues
– setTextAttributesForPositiveValues:
– textAttributesForPositiveValues
– setAttributedStringForZero:
– attributedStringForZero
– setTextAttributesForZero:
– textAttributesForZero
– setAttributedStringForNil:
– attributedStringForNil
– setTextAttributesForNil:
– textAttributesForNil
– setAttributedStringForNotANumber:
– attributedStringForNotANumber
– setTextAttributesForNotANumber:
– textAttributesForNotANumber
– setTextAttributesForPositiveInfinity:
– textAttributesForPositiveInfinity
– setTextAttributesForNegativeInfinity:
– textAttributesForNegativeInfinity
– setGroupingSeparator:
– groupingSeparator
– setUsesGroupingSeparator:
– usesGroupingSeparator
– setThousandSeparator:
– thousandSeparator
– setHasThousandSeparators:
– hasThousandSeparators
– setDecimalSeparator:
– decimalSeparator
– setAlwaysShowsDecimalSeparator:
– alwaysShowsDecimalSeparator
– setCurrencyDecimalSeparator:
– currencyDecimalSeparator
– setGroupingSize:
– groupingSize
– setSecondaryGroupingSize:
– secondaryGroupingSize
– setAllowsFloats:
– allowsFloats
– setMinimum:
– minimum
– setMaximum:
– maximum
– setMinimumIntegerDigits:
– minimumIntegerDigits
– setMinimumFractionDigits:
– minimumFractionDigits
– setMaximumIntegerDigits:
– maximumIntegerDigits
– setMaximumFractionDigits:
– maximumFractionDigits
– setUsesSignificantDigits:
– usesSignificantDigits
– setMinimumSignificantDigits:
– minimumSignificantDigits
– setMaximumSignificantDigits:
– maximumSignificantDigits
Returns an NSNumberFormatterBehavior
constant that indicates default formatter behavior for new instances of NSNumberFormatter
.
+ (NSNumberFormatterBehavior)defaultFormatterBehavior
An NSNumberFormatterBehavior
constant that indicates default formatter behavior for new instances of NSNumberFormatter
.
NSNumberFormatter.h
Sets the default formatter behavior for new instances of NSNumberFormatter
.
+ (void)setDefaultFormatterBehavior:(NSNumberFormatterBehavior)behavior
An NSNumberFormatterBehavior
constant that indicates the revision of the class providing the default behavior.
NSNumberFormatter.h
Returns a Boolean value that indicates whether the receiver allows floating-point values as input.
- (BOOL)allowsFloats
YES
if the receiver allows as input floating-point values (that is, values that include the period character [.
]), otherwise NO
.
When this method returns NO
, only integer values can be provided as input. The default is YES
.
NSNumberFormatter.h
Returns a Boolean value that indicates whether the receiver always shows a decimal separator, even if the number is an integer.
- (BOOL)alwaysShowsDecimalSeparator
YES
if the receiver always shows a decimal separator, even if the number is an integer, otherwise NO
.
NSNumberFormatter.h
Returns the attributed string used to display nil
values.
- (NSAttributedString *)attributedStringForNil
The attributed string used to display nil
values.
By default nil
values are displayed as an empty string.
This method is for use with formatters using NSNumberFormatterBehavior10_0
behavior.
NSNumberFormatter.h
Returns the attributed string used to display “not a number” values.
- (NSAttributedString *)attributedStringForNotANumber
The attributed string used to display “not a number” values.
By default “not a number” values are displayed as the string “NaN”.
This method is for use with formatters using NSNumberFormatterBehavior10_0
behavior.
NSNumberFormatter.h
Returns the attributed string used to display zero values.
- (NSAttributedString *)attributedStringForZero
The attributed string used to display zero values.
By default zero values are displayed according to the format specified for positive values; for more discussion of this subject see Data Formatting Programming Guide for Cocoa.
This method is for use with formatters using NSNumberFormatterBehavior10_0
behavior.
NSNumberFormatter.h
Returns the receiver’s currency code as a string.
- (NSString *)currencyCode
The receiver’s currency code as a string.
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.
NSNumberFormatter.h
Returns the receiver’s currency decimal separator as a string.
- (NSString *)currencyDecimalSeparator
The receiver’s currency decimal separator as a string.
NSNumberFormatter.h
Returns the currency grouping separator for the receiver.
- (NSString *)currencyGroupingSeparator
The currency grouping separator for the receiver.
NSNumberFormatter.h
Returns the receiver’s local currency symbol.
- (NSString *)currencySymbol
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.
NSNumberFormatter.h
Returns a string containing the character the receiver uses to represent decimal separators.
- (NSString *)decimalSeparator
A string containing the character the receiver uses to represent decimal separators.
The return value doesn’t indicate whether decimal separators are enabled.
NSNumberFormatter.h
Returns the string the receiver uses as an exponent symbol.
- (NSString *)exponentSymbol
The string the receiver uses as an exponent symbol.
The exponent symbol is the “E” or “e” in the scientific notation of numbers, as in 1.0e+56.
NSNumberFormatter.h
Returns the format used by the receiver.
- (NSString *)format
The format used by the receiver.
This method is for use with formatters using NSNumberFormatterBehavior10_0
behavior.
NSNumberFormatter.h
Returns an NSNumberFormatterBehavior
constant that indicates the formatter behavior of the receiver.
- (NSNumberFormatterBehavior)formatterBehavior
An NSNumberFormatterBehavior
constant that indicates the formatter behavior of the receiver.
NSNumberFormatter.h
Returns the format width of the receiver.
- (NSUInteger)formatWidth
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
.
NSNumberFormatter.h
Returns a Boolean value that indicates whether the receiver creates instances of NSDecimalNumber
when it converts strings to number objects.
- (BOOL)generatesDecimalNumbers
YES
if the receiver creates instances of NSDecimalNumber
when it converts strings to number objects, NO
if it creates instance of NSNumber
.
NSNumberFormatter.h
Returns by reference a cell-content object after creating it from a range of characters in a given string.
- (BOOL)getObjectValue:(out id *)anObject forString:(NSString *)aString range:(inout NSRange *)rangep error:(out NSError **)error
On return, contains an instance of NSDecimalNumber
or NSNumber
based on the current value of generatesDecimalNumbers
. The default is to return NSDecimalNumber
instances
A string object with the range of characters specified in rangep that is used to create anObject.
A range of characters in aString. On return, contains the actual range of characters used to create the object.
If an error occurs, upon return contains an NSError
object that explains the reason why the conversion failed. If you pass in nil
for error you are indicating that you are not interested in error information.
YES
if the conversion from string to cell-content object was successful, otherwise NO
.
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:
.
NSNumberFormatter.h
Returns a string containing the receiver’s grouping separator.
- (NSString *)groupingSeparator
A string containing the receiver’s grouping separator.
For example, the grouping separator used in the United States is the comma (“10,000”) whereas in France it is the period (“10.000”).
NSNumberFormatter.h
Returns the receiver’s primary grouping size.
- (NSUInteger)groupingSize
The receiver’s primary grouping size.
NSNumberFormatter.h
Returns a Boolean value that indicates whether the receiver’s format includes thousand separators.
- (BOOL)hasThousandSeparators
YES
if the receiver’s format includes thousand separators, otherwise NO
.
The default is NO
.
This method is for use with formatters using NSNumberFormatterBehavior10_0
behavior.
NSNumberFormatter.h
Returns the international currency symbol used by the receiver.
- (NSString *)internationalCurrencySymbol
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.
NSNumberFormatter.h
Returns a Boolean value that indicates whether the receiver uses heuristics to guess at the number which is intended by a string.
- (BOOL)isLenient
YES
if the receiver uses heuristics to guess at the number which is intended by the string; otherwise NO
.
NSNumberFormatter.h
Returns a Boolean value that indicates whether partial string validation is enabled.
- (BOOL)isPartialStringValidationEnabled
YES
if partial string validation is enabled, otherwise NO
.
NSNumberFormatter.h
Returns the locale of the receiver.
- (NSLocale *)locale
The locale of the receiver.
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.
NSNumberFormatter.h
Returns a Boolean value that indicates whether the receiver localizes formats.
- (BOOL)localizesFormat
YES
if the receiver localizes formats, otherwise NO
.
The default is NO
.
This method is for use with formatters using NSNumberFormatterBehavior10_0
behavior.
NSNumberFormatter.h
Returns the highest number allowed as input by the receiver.
- (NSNumber *)maximum
The highest number allowed as input by the receiver or nil
, meaning no limit.
For versions prior to Mac OS X v10.4 (and number-formatter behavior set to NSNumberFormatterBehavior10_0
) this method returns an NSDecimalNumber
object.
NSNumberFormatter.h
Returns the maximum number of digits after the decimal separator allowed as input by the receiver.
- (NSUInteger)maximumFractionDigits
The maximum number of digits after the decimal separator allowed as input by the receiver.
NSNumberFormatter.h
Returns the maximum number of integer digits allowed as input by the receiver.
- (NSUInteger)maximumIntegerDigits
The maximum number of integer digits allowed as input by the receiver.
NSNumberFormatter.h
Returns the maximum number of significant digits for the receiver.
- (NSUInteger)maximumSignificantDigits
The maximum number of significant digits for the receiver.
NSNumberFormatter.h
Returns the lowest number allowed as input by the receiver.
- (NSNumber *)minimum
The lowest number allowed as input by the receiver or nil
, meaning no limit.
For versions prior to Mac OS X v10.4 (and number-formatter behavior set to NSNumberFormatterBehavior10_0
) this method returns an NSDecimalNumber
object.
NSNumberFormatter.h
Returns the minimum number of digits after the decimal separator allowed as input by the receiver.
- (NSUInteger)minimumFractionDigits
The minimum number of digits after the decimal separator allowed as input by the receiver.
NSNumberFormatter.h
Returns the minimum number of integer digits allowed as input by the receiver.
- (NSUInteger)minimumIntegerDigits
The minimum number of integer digits allowed as input by the receiver.
NSNumberFormatter.h
Returns the minimum number of significant digits for the receiver.
- (NSUInteger)minimumSignificantDigits
The minimum number of significant digits for the receiver.
NSNumberFormatter.h
Returns the string the receiver uses to represent the minus sign.
- (NSString *)minusSign
The string that represents the receiver’s minus sign.
NSNumberFormatter.h
Returns the multiplier used by the receiver as an NSNumber
object.
- (NSNumber *)multiplier
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.
NSNumberFormatter.h
Returns the format used by the receiver to display negative numbers.
- (NSString *)negativeFormat
NSNumberFormatter.h
Returns the symbol the receiver uses to represent negative infinity.
- (NSString *)negativeInfinitySymbol
The symbol the receiver uses to represent negative infinity.
NSNumberFormatter.h
Returns the string the receiver inserts as a prefix to negative values.
- (NSString *)negativePrefix
The string the receiver inserts as a prefix to negative values.
NSNumberFormatter.h
Returns the string the receiver adds as a suffix to negative values.
- (NSString *)negativeSuffix
The string the receiver adds as a suffix to negative values.
NSNumberFormatter.h
Returns the string the receiver uses to represent a nil
value.
- (NSString *)nilSymbol
The string the receiver uses to represent a nil
value.
NSNumberFormatter.h
Returns the symbol the receiver uses to represent NaN (“not a number”) when it converts values.
- (NSString *)notANumberSymbol
The symbol the receiver uses to represent NaN (“not a number”) when it converts values.
NSNumberFormatter.h
Returns an NSNumber
object created by parsing a given string.
- (NSNumber *)numberFromString:(NSString *)string
An NSString
object that is parsed to generate the returned number object.
An NSNumber
object created by parsing string using the receiver’s format.
NSNumberFormatter.h
Returns the number-formatter style of the receiver.
- (NSNumberFormatterStyle)numberStyle
An NSNumberFormatterStyle
constant that indicates the number-formatter style of the receiver.
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.
NSNumberFormatter.h
Returns a string containing the padding character for the receiver.
- (NSString *)paddingCharacter
NSNumberFormatter.h
Returns the padding position of the receiver.
- (NSNumberFormatterPadPosition)paddingPosition
The returned constant indicates whether the padding is before or after the number’s prefix or suffix.
NSNumberFormatter.h
Returns the string that the receiver uses to represent the percent symbol.
- (NSString *)percentSymbol
NSNumberFormatter.h
Returns the string that the receiver uses for the per-thousands symbol.
- (NSString *)perMillSymbol
The string that the receiver uses for the per-thousands symbol.
NSNumberFormatter.h
Returns the string the receiver uses for the plus sign.
- (NSString *)plusSign
The string the receiver uses for the plus sign.
NSNumberFormatter.h
Returns the format used by the receiver to display positive numbers.
- (NSString *)positiveFormat
NSNumberFormatter.h
Returns the string the receiver uses for the positive infinity symbol.
- (NSString *)positiveInfinitySymbol
The string the receiver uses for the positive infinity symbol.
NSNumberFormatter.h
Returns the string the receiver uses as the prefix for positive values.
- (NSString *)positivePrefix
The string the receiver uses as the prefix for positive values.
NSNumberFormatter.h
Returns the string the receiver uses as the suffix for positive values.
- (NSString *)positiveSuffix
The string the receiver uses as the suffix for positive values.
NSNumberFormatter.h
Returns an NSDecimalNumberHandler
object indicating the rounding behavior of the receiver.
- (NSDecimalNumberHandler *)roundingBehavior
An NSDecimalNumberHandler
object indicating the rounding behavior of the receiver.
This method is for use with formatters using NSNumberFormatterBehavior10_0
behavior.
NSNumberFormatter.h
Returns the rounding increment used by the receiver.
- (NSNumber *)roundingIncrement
The rounding increment used by the receiver.
NSNumberFormatter.h
Returns the rounding mode used by the receiver.
- (NSNumberFormatterRoundingMode)roundingMode
The rounding mode used by the receiver.
NSNumberFormatter.h
Returns the size of secondary groupings for the receiver.
- (NSUInteger)secondaryGroupingSize
The size of secondary groupings for the receiver.
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.
NSNumberFormatter.h
Sets whether the receiver allows as input floating-point values (that is, values that include the period character [.
]).
- (void)setAllowsFloats:(BOOL)flag
YES
if the receiver allows floating-point values, NO
otherwise.
By default, floating point values are allowed as input.
NSNumberFormatter.h
Controls whether the receiver always shows the decimal separator, even for integer numbers.
- (void)setAlwaysShowsDecimalSeparator:(BOOL)flag
YES
if the receiver should always show the decimal separator, NO
otherwise.
NSNumberFormatter.h
Sets the attributed string the receiver uses to display nil
values.
- (void)setAttributedStringForNil:(NSAttributedString *)newAttributedString
An NSAttributedString
object that the receiver uses to display nil
values.
This method is for use with formatters using NSNumberFormatterBehavior10_0
behavior.
NSNumberFormatter.h
Sets the attributed string the receiver uses to display “not a number” values.
- (void)setAttributedStringForNotANumber:(NSAttributedString *)newAttributedString
An NSAttributedString
object that the receiver uses to display NaN values.
This method is for use with formatters using NSNumberFormatterBehavior10_0
behavior.
NSNumberFormatter.h
Sets the attributed string that the receiver uses to display zero values.
- (void)setAttributedStringForZero:(NSAttributedString *)newAttributedString
An NSAttributedString
object that the receiver uses to display zero values.
This method is for use with formatters using NSNumberFormatterBehavior10_0
behavior.
NSNumberFormatter.h
Sets the receiver’s currency code.
- (void)setCurrencyCode:(NSString *)string
A string specifying the receiver's new currency code.
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.
NSNumberFormatter.h
Sets the string used by the receiver as a decimal separator.
- (void)setCurrencyDecimalSeparator:(NSString *)string
The string to use as the currency decimal separator.
NSNumberFormatter.h
Sets the currency grouping separator for the receiver.
- (NSString *)setCurrencyGroupingSeparator:(NSString *)string
The currency grouping separator for the receiver.
NSNumberFormatter.h
Sets the string used by the receiver as a local currency symbol.
- (void)setCurrencySymbol:(NSString *)string
A string that represents a local 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.
NSNumberFormatter.h
Sets the character the receiver uses as a decimal separator.
- (void)setDecimalSeparator:(NSString *)newSeparator
The string that specifies the decimal-separator character to use. If newSeparator contains multiple characters, only the first one is used.
If you don’t have decimal separators enabled through another means (such as setFormat:
), using this method enables them.
NSNumberFormatter.h
Sets the string used by the receiver to represent the exponent symbol.
- (void)setExponentSymbol:(NSString *)string
A string that represents an exponent symbol.
The exponent symbol is the “E” or “e” in the scientific notation of numbers, as in 1.0e+56.
NSNumberFormatter.h
Sets the receiver’s format.
- (void)setFormat:(NSString *)aFormat
A string that can consist of one, two, or three parts separated by “;”. The first part of the string represents the positive format, the second part of the string represents the zero value, and the last part of the string represents the negative format. If the string has just two parts, the first one becomes the positive format, and the second one becomes the negative format. If the string has just one part, it becomes the positive format, and default formats are provided for zero and negative values based on the positive format. For more discussion of this subject, see Data Formatting Programming Guide for Cocoa.
The following code excerpt shows the three different approaches for setting an NSNumberFormatter
object’s format using setFormat:
:
NSNumberFormatter *numberFormatter = |
[[[NSNumberFormatter alloc] init] autorelease]; |
// specify just positive format |
[numberFormatter setFormat:@"$#,##0.00"]; |
// specify positive and negative formats |
[numberFormatter setFormat:@"$#,##0.00;($#,##0.00)"]; |
// specify positive, zero, and negative formats |
[numberFormatter setFormat:@"$#,###.00;0.00;($#,##0.00)"]; |
This method is for use with formatters using NSNumberFormatterBehavior10_0
behavior.
NSNumberFormatter.h
Sets the formatter behavior of the receiver.
- (void)setFormatterBehavior:(NSNumberFormatterBehavior)behavior
An NSNumberFormatterBehavior
constant that indicates the revision of the NSNumberFormatter
class providing the current behavior.
NSNumberFormatter.h
Sets the format width used by the receiver.
- (void)setFormatWidth:(NSUInteger)number
An integer that specifies the format width.
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
.
NSNumberFormatter.h
Controls whether the receiver creates instances of NSDecimalNumber
when it converts strings to number objects.
- (void)setGeneratesDecimalNumbers:(BOOL)flag
YES
if the receiver should generate NSDecimalNumber
instances, NO
if it should generate NSNumber
instances.
The default is YES
.
NSNumberFormatter.h
Specifies the string used by the receiver for a grouping separator.
- (void)setGroupingSeparator:(NSString *)string
A string that specifies the grouping separator to use.
NSNumberFormatter.h
Sets the grouping size of the receiver.
- (void)setGroupingSize:(NSUInteger)numDigits
An integer that specifies the grouping size.
NSNumberFormatter.h
Sets whether the receiver uses thousand separators.
- (void)setHasThousandSeparators:(BOOL)flag
When flag is NO
, thousand separators are disabled for both positive and negative formats (even if you’ve set them through another means, such as setFormat:
). When flag is YES
, thousand separators are used.
In addition to using this method to add thousand separators to your format, you can also use it to disable thousand separators if you’ve set them using another method. The default is NO
(though you in effect change this setting to YES
when you set thousand separators through any means, such as setFormat:
).
This method is for use with formatters using NSNumberFormatterBehavior10_0
behavior.
NSNumberFormatter.h
Sets the string used by the receiver for the international currency symbol.
- (void)setInternationalCurrencySymbol:(NSString *)string
A string that represents 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.
NSNumberFormatter.h
Sets whether the receiver will use heuristics to guess at the number which is intended by a string.
- (void)setLenient:(BOOL)b
YES
if the receiver will use heuristics to guess at the number which is intended by the string; otherwise NO
.
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).
NSNumberFormatter.h
Sets the locale of the receiver.
- (void)setLocale:(NSLocale *)theLocale
An NSLocale
object representing the new locale of the receiver.
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.
NSNumberFormatter.h
Sets whether the dollar sign character ($
), decimal separator character (.
), and thousand separator character (,
) are converted to appropriately localized characters as specified by the user’s localization preference.
- (void)setLocalizesFormat:(BOOL)flag
YES
if these characters are converted to the localized equivalents, NO
otherwise.
While the currency-symbol part of this feature may be useful in certain types of applications, it’s probably more likely that you would tie a particular application to a particular currency (that is, that you would “hard-code” the currency symbol and separators instead of having them dynamically change based on the user’s configuration). The reason for this, of course, is that NSNumberFormatter
doesn’t perform currency conversions, it just formats numeric data. You wouldn’t want one user interpreting the value "56324"
as US currency and another user who’s accessing the same data interpreting it as Japanese currency, simply based on each user’s localization preferences.
This method is for use with formatters using NSNumberFormatterBehavior10_0
behavior.
NSNumberFormatter.h
Sets the highest number the receiver allows as input.
- (void)setMaximum:(NSNumber *)aMaximum
A number object that specifies a maximum input value.
If aMaximum is nil
, checking for the maximum value is disabled. For versions prior to Mac OS X v10.4 (and number-formatter behavior set to NSNumberFormatterBehavior10_0
) this method requires an NSDecimalNumber
argument.
NSNumberFormatter.h
Sets the maximum number of digits after the decimal separator allowed as input by the receiver.
- (void)setMaximumFractionDigits:(NSUInteger)number
The maximum number of digits after the decimal separator allowed as input.
NSNumberFormatter.h
Sets the maximum number of integer digits allowed as input by the receiver.
- (void)setMaximumIntegerDigits:(NSUInteger)number
The maximum number of integer digits allowed as input.
NSNumberFormatter.h
Sets the maximum number of significant digits for the receiver.
- (void)setMaximumSignificantDigits:(NSUInteger)number
The maximum number of significant digits for the receiver.
NSNumberFormatter.h
Sets the lowest number the receiver allows as input.
- (void)setMinimum:(NSNumber *)aMinimum
A number object that specifies a minimum input value.
If aMinimum is nil
, checking for the minimum value is disabled. For versions prior to Mac OS X v10.4 (and number-formatter behavior set to NSNumberFormatterBehavior10_0
) this method requires an NSDecimalNumber
argument.
NSNumberFormatter.h
Sets the minimum number of digits after the decimal separator allowed as input by the receiver.
- (void)setMinimumFractionDigits:(NSUInteger)number
The minimum number of digits after the decimal separator allowed as input.
NSNumberFormatter.h
Sets the minimum number of integer digits allowed as input by the receiver.
- (void)setMinimumIntegerDigits:(NSUInteger)number
The minimum number of integer digits allowed as input.
NSNumberFormatter.h
Sets the minimum number of significant digits for the receiver.
- (void)setMinimumSignificantDigits:(NSUInteger)number
The minimum number of significant digits for the receiver.
NSNumberFormatter.h
Sets the string used by the receiver for the minus sign.
- (void)setMinusSign:(NSString *)string
A string that represents a minus sign.
NSNumberFormatter.h
Sets the multiplier of the receiver.
- (void)setMultiplier:(NSNumber *)number
A number object that represents a multiplier.
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.
NSNumberFormatter.h
Sets the format the receiver uses to display negative values.
- (void)setNegativeFormat:(NSString *)aFormat
A string that specifies the format for negative values.
NSNumberFormatter.h
Sets the string used by the receiver for the negative infinity symbol.
- (void)setNegativeInfinitySymbol:(NSString *)string
A string that represents a negative infinity symbol.
NSNumberFormatter.h
Sets the string the receiver uses as a prefix for negative values.
- (void)setNegativePrefix:(NSString *)string
A string to use as the prefix for negative values.
NSNumberFormatter.h
Sets the string the receiver uses as a suffix for negative values.
- (void)setNegativeSuffix:(NSString *)string
A string to use as the suffix for negative values.
NSNumberFormatter.h
Sets the string the receiver uses to represent nil
values.
- (void)setNilSymbol:(NSString *)string
A string that represents a nil
value.
NSNumberFormatter.h
Sets the string the receiver uses to represent NaN (“not a number”).
- (void)setNotANumberSymbol:(NSString *)string
A string that represents a NaN symbol.
NSNumberFormatter.h
Sets the number style used by the receiver.
- (void)setNumberStyle:(NSNumberFormatterStyle)style
An NSNumberFormatterStyle
constant that specifies a formatter style.
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.
NSNumberFormatter.h
Sets the string that the receiver uses to pad numbers in the formatted string representation.
- (void)setPaddingCharacter:(NSString *)string
A string containing a padding character (or characters).
NSNumberFormatter.h
Sets the padding position used by the receiver.
- (void)setPaddingPosition:(NSNumberFormatterPadPosition)position
An NSNumberFormatterPadPosition
constant that indicates a padding position (before or after prefix or suffix).
NSNumberFormatter.h
Sets whether partial string validation is enabled for the receiver.
- (void)setPartialStringValidationEnabled:(BOOL)b
YES
if partial string validation is enabled, otherwise NO
.
NSNumberFormatter.h
Sets the string used by the receiver to represent the percent symbol.
- (void)setPercentSymbol:(NSString *)string
A string that represents a percent symbol.
NSNumberFormatter.h
Sets the string used by the receiver to represent the per-mill (per-thousand) symbol.
- (void)setPerMillSymbol:(NSString *)string
A string that represents a per-mill symbol.
NSNumberFormatter.h
Sets the string used by the receiver to represent the plus sign.
- (void)setPlusSign:(NSString *)string
A string that represents a plus sign.
NSNumberFormatter.h
Sets the format the receiver uses to display positive values.
- (void)setPositiveFormat:(NSString *)aFormat
A string that specifies the format for positive values.
NSNumberFormatter.h
Sets the string used by the receiver for the positive infinity symbol.
- (void)setPositiveInfinitySymbol:(NSString *)string
A string that represents a positive infinity symbol.
NSNumberFormatter.h
Sets the string the receiver uses as the prefix for positive values.
- (void)setPositivePrefix:(NSString *)string
A string to use as the prefix for positive values.
NSNumberFormatter.h
Sets the string the receiver uses as the suffix for positive values.
- (void)setPositiveSuffix:(NSString *)string
A string to use as the suffix for positive values.
NSNumberFormatter.h
Sets the rounding behavior used by the receiver.
- (void)setRoundingBehavior:(NSDecimalNumberHandler *)newRoundingBehavior
An NSDecimalNumberHandler
object representing a rounding behavior.
This method is for use with formatters using NSNumberFormatterBehavior10_0
behavior.
NSNumberFormatter.h
Sets the rounding increment used by the receiver.
- (void)setRoundingIncrement:(NSNumber *)number
A number object specifying a rounding increment.
NSNumberFormatter.h
Sets the rounding mode used by the receiver.
- (void)setRoundingMode:(NSNumberFormatterRoundingMode)mode
An NSNumberFormatterRoundingMode
constant that indicates a rounding mode.
NSNumberFormatter.h
Sets the secondary grouping size of the receiver.
- (void)setSecondaryGroupingSize:(NSUInteger)number
An integer that specifies the size of secondary groupings.
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.
NSNumberFormatter.h
Sets the text attributes used to display the negative infinity symbol.
- (void)setTextAttributesForNegativeInfinity:(NSDictionary *)newAttributes
A dictionary containing text attributes for the display of the negative infinity symbol.
NSNumberFormatter.h
Sets the text attributes to be used in displaying negative values .
- (void)setTextAttributesForNegativeValues:(NSDictionary *)newAttributes
A dictionary containing properties for the display of negative values.
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.
NSNumberFormatter.h
Sets the text attributes used to display the nil
symbol.
- (void)setTextAttributesForNil:(NSDictionary *)newAttributes
A dictionary containing text attributes for the display of the nil
symbol.
NSNumberFormatter.h
Sets the text attributes used to display the NaN ("not a number") string.
- (void)setTextAttributesForNotANumber:(NSDictionary *)newAttributes
A dictionary containing text attributes for the display of the NaN symbol.
NSNumberFormatter.h
Sets the text attributes used to display the positive infinity symbol.
- (void)setTextAttributesForPositiveInfinity:(NSDictionary *)newAttributes
A dictionary containing text attributes for the display of the positive infinity symbol.
NSNumberFormatter.h
Sets the text attributes to be used in displaying positive values.
- (void)setTextAttributesForPositiveValues:(NSDictionary *)newAttributes
A dictionary containing text attributes for the display of positive values.
See setTextAttributesForNegativeValues: for an example of how a related method might be used.
NSNumberFormatter.h
Sets the text attributes used to display a zero value.
- (void)setTextAttributesForZero:(NSDictionary *)newAttributes
A dictionary containing text attributes for the display of zero values.
NSNumberFormatter.h
Sets the character the receiver uses as a thousand separator.
- (void)setThousandSeparator:(NSString *)newSeparator
A string that specifies the thousand-separator character to use. If newSeparator contains multiple characters, only the first one is used.
If you don’t have thousand separators enabled through any other means (such as setFormat:
), using this method enables them.
This method is for use with formatters using NSNumberFormatterBehavior10_0
behavior.
NSNumberFormatter.h
Controls whether the receiver displays the grouping separator.
- (void)setUsesGroupingSeparator:(BOOL)flag
YES
if the receiver should display the grouping separator, NO
otherwise.
NSNumberFormatter.h
Sets whether the receiver uses significant digits.
- (void)setUsesSignificantDigits:(BOOL)b
YES
if the receiver uses significant digits, otherwise NO
.
NSNumberFormatter.h
Sets the string the receiver uses as the symbol to show the value zero.
- (void)setZeroSymbol:(NSString *)string
The string the receiver uses as the symbol to show the value zero.
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.
On Mac 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.
NSNumberFormatter.h
Returns a string containing the formatted value of the provided number object.
- (NSString *)stringFromNumber:(NSNumber *)number
An NSNumber
object that is parsed to create the returned string object.
A string containing the formatted value of number using the receiver’s current settings.
NSNumberFormatter.h
Returns a dictionary containing the text attributes used to display the negative infinity string.
- (NSDictionary *)textAttributesForNegativeInfinity
A dictionary containing the text attributes used to display the negative infinity string.
NSNumberFormatter.h
Returns a dictionary containing the text attributes that have been set for negative values.
- (NSDictionary *)textAttributesForNegativeValues
A dictionary containing the text attributes that have been set for negative values.
NSNumberFormatter.h
Returns a dictionary containing the text attributes used to display the nil
symbol.
- (NSDictionary *)textAttributesForNil
A dictionary containing the text attributes used to display the nil
symbol.
NSNumberFormatter.h
Returns a dictionary containing the text attributes used to display the NaN ("not a number") symbol.
- (NSDictionary *)textAttributesForNotANumber
A dictionary containing the text attributes used to display the NaN ("not a number") symbol.
NSNumberFormatter.h
Returns a dictionary containing the text attributes used to display the positive infinity symbol.
- (NSDictionary *)textAttributesForPositiveInfinity
A dictionary containing the text attributes used to display the positive infinity symbol.
NSNumberFormatter.h
Returns a dictionary containing the text attributes that have been set for positive values.
- (NSDictionary *)textAttributesForPositiveValues
A dictionary containing the text attributes that have been set for positive values.
NSNumberFormatter.h
Returns a dictionary containing the text attributes used to display a value of zero.
- (NSDictionary *)textAttributesForZero
A dictionary containing the text attributes used to display a value of zero.
NSNumberFormatter.h
Returns a string containing the character the receiver uses to represent thousand separators.
- (NSString *)thousandSeparator
A string containing the character the receiver uses to represent thousand separators.
By default this is the comma character (,
). Note that the return value doesn’t indicate whether thousand separators are enabled.
This method is for use with formatters using NSNumberFormatterBehavior10_0
behavior.
NSNumberFormatter.h
Returns a Boolean value that indicates whether the receiver uses the grouping separator.
- (BOOL)usesGroupingSeparator
YES
if the receiver uses the grouping separator, otherwise NO
.
NSNumberFormatter.h
Returns a Boolean value that indicates whether the receiver uses significant digits.
- (BOOL)usesSignificantDigits
YES
if the receiver uses significant digits, otherwise NO
.
NSNumberFormatter.h
Returns the string the receiver uses as the symbol to show the value zero.
- (NSString *)zeroSymbol
The string the receiver uses as the symbol to show the value zero.
For a discussion of how this is used, see setZeroSymbol:
.
NSNumberFormatter.h
These constants specify predefined number format styles.
typedef enum { NSNumberFormatterNoStyle = kCFNumberFormatterNoStyle, NSNumberFormatterDecimalStyle = kCFNumberFormatterDecimalStyle, NSNumberFormatterCurrencyStyle = kCFNumberFormatterCurrencyStyle, NSNumberFormatterPercentStyle = kCFNumberFormatterPercentStyle, NSNumberFormatterScientificStyle = kCFNumberFormatterScientificStyle, NSNumberFormatterSpellOutStyle = kCFNumberFormatterSpellOutStyle } NSNumberFormatterStyle;
NSNumberFormatterNoStyle
Specifies no style.
Available in Mac OS X v10.4 and later.
Declared in NSNumberFormatter.h
.
NSNumberFormatterDecimalStyle
Specifies a decimal style format.
Available in Mac OS X v10.4 and later.
Declared in NSNumberFormatter.h
.
NSNumberFormatterCurrencyStyle
Specifies a currency style format.
Available in Mac OS X v10.4 and later.
Declared in NSNumberFormatter.h
.
NSNumberFormatterPercentStyle
Specifies a percent style format.
Available in Mac OS X v10.4 and later.
Declared in NSNumberFormatter.h
.
NSNumberFormatterScientificStyle
Specifies a scientific style format.
Available in Mac OS X v10.4 and later.
Declared in NSNumberFormatter.h
.
NSNumberFormatterSpellOutStyle
Specifies a spell-out format; for example, “23” becomes “twenty-three”.
Available in Mac OS X v10.4 and later.
Declared in NSNumberFormatter.h
.
These constants are used by the numberStyle
and setNumberStyle:
methods.
NSNumberFormatter.h
These constants specify the behavior of a number formatter.
typedef enum { NSNumberFormatterBehaviorDefault = 0, NSNumberFormatterBehavior10_0 = 1000, NSNumberFormatterBehavior10_4 = 1040, } NSNumberFormatterBehavior;
NSNumberFormatterBehaviorDefault
The number-formatter behavior set as the default for new instances. You can set the default formatter behavior with the class method setDefaultFormatterBehavior:
.
Available in Mac OS X v10.4 and later.
Declared in NSNumberFormatter.h
.
NSNumberFormatterBehavior10_0
The number-formatter behavior as it existed prior to Mac OS X v10.4.
Available in Mac OS X v10.4 and later.
Declared in NSNumberFormatter.h
.
NSNumberFormatterBehavior10_4
The number-formatter behavior since Mac OS X v10.4.
Available in Mac OS X v10.4 and later.
Declared in NSNumberFormatter.h
.
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.
NSNumberFormatter.h
These constants are used to specify how numbers should be padded.
typedef enum { NSNumberFormatterPadBeforePrefix = kCFNumberFormatterPadBeforePrefix, NSNumberFormatterPadAfterPrefix = kCFNumberFormatterPadAfterPrefix, NSNumberFormatterPadBeforeSuffix = kCFNumberFormatterPadBeforeSuffix, NSNumberFormatterPadAfterSuffix = kCFNumberFormatterPadAfterSuffix } NSNumberFormatterPadPosition;
NSNumberFormatterPadBeforePrefix
Specifies that the padding should occur before the prefix.
Available in Mac OS X v10.4 and later.
Declared in NSNumberFormatter.h
.
NSNumberFormatterPadAfterPrefix
Specifies that the padding should occur after the prefix.
Available in Mac OS X v10.4 and later.
Declared in NSNumberFormatter.h
.
NSNumberFormatterPadBeforeSuffix
Specifies that the padding should occur before the suffix.
Available in Mac OS X v10.4 and later.
Declared in NSNumberFormatter.h
.
NSNumberFormatterPadAfterSuffix
Specifies that the padding should occur after the suffix.
Available in Mac OS X v10.4 and later.
Declared in NSNumberFormatter.h
.
These constants are used by the paddingPosition
and setPaddingPosition:
methods.
NSNumberFormatter.h
These constants are used to specify how numbers should be rounded.
typedef enum { NSNumberFormatterRoundCeiling = kCFNumberFormatterRoundCeiling, NSNumberFormatterRoundFloor = kCFNumberFormatterRoundFloor, NSNumberFormatterRoundDown = kCFNumberFormatterRoundDown, NSNumberFormatterRoundUp = kCFNumberFormatterRoundUp, NSNumberFormatterRoundHalfEven = kCFNumberFormatterRoundHalfEven, NSNumberFormatterRoundHalfDown = kCFNumberFormatterRoundHalfDown, NSNumberFormatterRoundHalfUp = kCFNumberFormatterRoundHalfUp } NSNumberFormatterRoundingMode;
NSNumberFormatterRoundCeiling
Round up to next larger number with the proper number of digits after the decimal separator.
Available in Mac OS X v10.4 and later.
Declared in NSNumberFormatter.h
.
NSNumberFormatterRoundFloor
Round down to next smaller number with the proper number of digits after the decimal separator.
Available in Mac OS X v10.4 and later.
Declared in NSNumberFormatter.h
.
NSNumberFormatterRoundDown
Round down to next smaller number with the proper number of digits after the decimal separator.
Available in Mac OS X v10.4 and later.
Declared in NSNumberFormatter.h
.
NSNumberFormatterRoundHalfEven
Round the last digit, when followed by a 5, toward an even digit (.25 -> .2, .35 -> .4)
Available in Mac OS X v10.4 and later.
Declared in NSNumberFormatter.h
.
NSNumberFormatterRoundUp
Round up to next larger number with the proper number of digits after the decimal separator.
Available in Mac OS X v10.4 and later.
Declared in NSNumberFormatter.h
.
NSNumberFormatterRoundHalfDown
Round down when a 5 follows putative last digit.
Available in Mac OS X v10.4 and later.
Declared in NSNumberFormatter.h
.
NSNumberFormatterRoundHalfUp
Round up when a 5 follows putative last digit.
Available in Mac OS X v10.4 and later.
Declared in NSNumberFormatter.h
.
NSNumberFormatter.h
These constants are used by the roundingMode
and setRoundingMode:
methods.
© 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-11-19)