PATH |
- Inherits from:
- java.text.Format : Object
- Package:
- com.webobjects.foundation
Instances of NSNumberFormatter convert between BigDecimal numbers and textual representations of numeric values. The representation encompasses integers and floating-point numbers; floating-point numbers can be formatted to a specified decimal position. NSNumberFormatters can also impose ranges on the numeric values that can be formatted.
You can associate a number pattern with a WOString or WOTextField dynamic element. WebObjects uses an NSNumberFormatter object to perform the appropriate conversions.
Instances of NSNumberFormatter are mutable.
The most common technique for creating a NSNumberFormatter is to use the one-argument constructor, which takes as its argument a string whose contents can be one of the following:
"$###,##0.00"
(the syntax of format strings is discussed in the following section).
"###,##0.00;(###,##0.00)"
.
"$###,###.00;0.00;($###,##0.00)"
. Note that zero patterns are treated as string constants.
As implied in the above list, you're only required to specify a pattern for positive values. If you don't specify a pattern for negative and zero values, a default pattern based on the positive value pattern is used. For example, if your positive value pattern is "#,##0.00"
, an input value of "0"
will be displayed as "0.00"
.
If you don't specify a pattern for negative values, the pattern specified for positive values is used, preceded by a minus sign (-
).
If you specify a separate pattern for negative values, its separators should be parallel to those specified in the positive pattern string. In NSNumberFormatter, separators are either enabled or disabled for all patterns-both your negative and positive patterns should therefore use the same approach.
As an alternative to using the one-argument constructor is to use the no-argument constructor and invoking setPattern with the pattern. You can also use the setPositivePattern and setNegativePattern methods.
Pattern strings can include the following types of characters:
"9,990.00"
, and the value 53.88 is entered into a cell to which the pattern has been applied. The cell would display the value as 9,953.88.
.
) as a decimal separator, and comma character (,
) as a thousand separator. If you want to use different characters as separators, you can set them using the setDecimalSeparator and setThousandSeparator methods.
#
) to represent numeric characters that will be input by the user. For example, suppose you have the positive pattern "$#,##0.00"
. If the characters 76329 were entered into a cell to which the pattern has been applied, they would be displayed as $76,329.00. Strictly speaking, however, you don't need to use placeholders. The format strings ",0.00"
, "#,#0.00"
, and "#,##0.00"
are functionally equivalent. In other words, including separator characters in a pattern string signals NSNumberFormatter to use the separators, regardless of whether you use (or where you put) placeholders. The placeholder character's chief virtue lies in its ability to make pattern strings more human-readable, which is especially useful if you're displaying patterns in the user interface.
_
). This character inserts a space if no numeric character has been input to occupy that position.
$
) is normally treated just like any other character that doesn't play a special role in NSNumberFormatter.
All other characters specified in a pattern string are displayed as typed. The following table shows examples of the how the value 1019.55 is displayed for different positive patterns:
Pattern String | Display |
"#,##0.00" |
1,019.55 |
"$#,##0.00" |
$1,019.55 |
"___,__0.00" |
1,019.55 |
NSNumberFormatter supports two different kinds of separators: thousand and decimal. By default these separators are represented by the comma (,
) and period (.
) characters respectively. The default pattern ("#,##0.##
") enables them.
All of the following statements have the effect of enabling thousand separators:
// use setPattern: numberFormatter.setPattern("#,###"); // use setHasThousandSeparators: numberFormatter.setHasThousandSeparators(true); // use setThousandSeparator: numberFormatter.setThousandSeparator("_");
If you use the statement numberFormatter.setHasThousandSeparators(false)
, it disables thousand separators, even if you've set them through another means.
Both of the following statements have the effect of enabling decimal separators:
// use setFormat: numberFormatter.setFormat("0.00"); // use setDecimalSeparator: numberFormatter.setDecimalSeparator("-");
When you enable or disable separators, it affects both positive and negative patterns. Consequently, both patterns must use the same separator scheme.
You can use the thousandSeparator and decimalSeparator methods to return a string containing the character the receiver uses to represent each separator. However, this shouldn't be taken as an indication of whether separators are enabled-even when separators are disabled, an NSNumberFormatter still knows the characters it uses to represent separators.
Separators must be single characters. If you specify multiple characters in the arguments to setThousandSeparator and setDecimalSeparator, only the first character is used.
You can't use the same character to represent thousand and decimal separators.
NSNumberFormatter provides methods to localize pattern strings. You can change the currency symbol, the decimal separator, and the thousands separator manually, or you can trust NSNumberFormatter to do it for you, based on locales. If you enable localization for an instance of NSNumberFormatter, it will check the current locale and localize pattern strings appropriately for that locale. By default, instances of NSNumberFormatter are not localized. You can enable localization for all new instances of NSNumberFormatter using setDefaultLocalizesPattern or for a specific instance of NSNumberFormatter using setLocalizesPattern. See the method descriptions for setDefaultLocalizesPattern and setLocalizesPattern for more information.
NSNumberFormatter provides the following constants for specifying rounding modes and special numbers.The rounding mode specifiers are used with roundingBehavior and setRoundingBehavior.
Constant | Type | Description |
RoundDown | int |
Rounding mode specifier: round towards negative infinity |
RoundUp | int |
Rounding mode specifier: round towards positive infinity |
RoundPlain | int |
Rounding mode specifier: round to nearest up |
RoundBankers | int |
Rounding mode specifier: round to nearest even |
NSDecimalNotANumber | java.math.BigDecimal |
Definition of Not A Number (NaN) |
DefaultPattern | String |
Default format for pattern strings: "#,##0.##". |
- Constructors
- NSNumberFormatter
- Performing formatted conversions
- objectValueForString
- stringForObjectValue
- Methods inherited from java.text.Format
- format
- parseObject
- Accessing patterns
- negativePattern
- pattern
- positivePattern
- setNegativePattern
- setPattern
- setPositivePattern
- Accessing floating point settings
- allowsFloats
- roundingBehavior
- setAllowsFloats
- setRoundingBehavior
- Accessing decimal separator settings
- decimalSeparator
- setDecimalSeparator
- Accessing thousand separator settings
- hasThousandSeparators
- setHasThousandSeparators
- setThousandSeparator
- thousandSeparator
- Accessing strings for special numbers
- setStringForNotANumber
- setStringForNull
- setStringForZero
- stringForNotANumber
- stringForNull
- stringForZero
- Limiting the input number
- maximum
- minimum
- setMaximum
- setMinimum
- Localization Methods
- availableLocales
- currencySymbol
- defaultLocale
- defaultLocalizesPattern
- locale
- localizesPattern
- setCurrencySymbol
- setDefaultLocale
- setDefaultLocalizesPattern
- setLocale
- setLocalizesPattern
- Deprecated Methods
- attributedStringForNil
- attributedStringForNotANumber
- attributedStringForZero
- format
- localizesFormat
- negativeFormat
- positiveFormat
- setAttributedStringForNil
- setAttributedStringForNotANumber
- setAttributedStringForZero
- setFormat
- setLocalizesFormat
- setNegativeFormat
- setPositiveFormat
public NSNumberFormatter()
#,##0.##
".
public NSNumberFormatter(String pattern)
See Also: setPattern
public static java.util.Locale[] availableLocales()
public static java.util.Locale defaultLocale()
public static boolean defaultLocalizesPattern()
true
to indicate that the receiver's format will be localized for all new instances of NSNumberFormatter in your application. By default, patterns are not localized.
See Also: setDefaultLocalizesPattern
public static void setDefaultLocale(Locale newLocale)
public static void setDefaultLocalizesPattern(boolean newDefault)
true
. By default, NSNumberFormatters are not localized.
See Also: defaultLocalizesPattern
public boolean allowsFloats()
true
if the receiver allows as input floating point values (that is, values that include the period character (.
)), false
otherwise. When this is set to false
, only integer values can be provided as input. The default is true
.
public String attributedStringForNil()
public String attributedStringForNotANumber()
public String attributedStringForZero()
public String currencySymbol()
See Also: setCurrencySymbol
public String decimalSeparator()
.
).
public StringBuffer format( Object object, StringBuffer toAppendTo, java.text.FieldPosition position)
See Also: stringForObjectValue
public String format()
public boolean hasThousandSeparators()
true
to indicate that the receiver's format includes thousand separators, false
otherwise. The default is false
.
public java.util.Locale locale()
public boolean localizesFormat()
public boolean localizesPattern()
true
to indicate that the receiver's format will be localized for a specific instance of NSNumberFormatter. By default, instances of NSNumberFormatter are not localized.
public java.math.BigDecimal maximum()
public java.math.BigDecimal minimum()
public String negativeFormat()
public String negativePattern()
See Also: positivePattern, setPattern
public Object objectValueForString(String inString) throws java.text.ParseException
See Also: setPattern
public Object parseObject( String source, java.text.ParsePosition status)
public Object parseObject(String source) throws java.text.ParseException
See Also: objectValueForString
public String pattern()
See Also: setPattern
public String positiveFormat()
public String positivePattern()
See Also: setPattern, setNegativePattern
public int roundingBehavior()
public void setAllowsFloats(boolean flag)
.
)). By default, floating point values are allowed as input.
public void setAttributedStringForNil(String string)
public void setAttributedStringForNotANumber(String string)
public void setAttributedStringForZero(String string)
public void setCurrencySymbol(String newSymbol)
See Also: currencySymbol
public void setDecimalSeparator(String aString)
public void setFormat(String format)
public void setHasThousandSeparators(boolean flag)
false
, thousand separators are disabled for both positive and negative formats (even if you've set them through another means, such as setPattern). When flag is true
, 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 false
(though you in effect change this setting to true
when you set thousand separators through any means, such as setPattern)
public void setLocale(java.util.Locale newLocale)
public void setLocalizesFormat(boolean newDefault)
public void setLocalizesPattern(boolean newDefault)
true
. By default, NSNumberFormatters are not localized.
public void setMaximum(java.math.BigDecimal aMaximum)
public void setMinimum(java.math.BigDecimal aMinimum)
public void setNegativeFormat(String format)
public void setNegativePattern(String aString)
validatePattern()
which throws an IllegalArgumentException if the pattern is null, the pattern string is empty, or the string does not contain one of the characters in ",._#0123456789".
See Also: setPattern
public void setPattern(String aPattern)
";"
. The first part of the string represents the positive pattern, the second part of the string represents the zero value, and the last part of the string represents the negative pattern. If the string just has two parts, the first one becomes the positive pattern, and the second one becomes the negative pattern. If the string just has one part, it becomes the positive pattern, and default formats are provided for zero and negative values based on the positive format. For more discussion of this subject, see the section "Creating an Instance of NSNumberFormatter" (page 207) in the Class Description. If the positive, negative, or zero pattern is illegal, the method throws an IllegalArgumentException. See "Pattern String Syntax" (page 208) for an explanation of what makes a pattern legal.
The following code excerpt shows the three different approaches for setting an NSNumberFormatter object's format using setPattern:
NSNumberFormatter numberFormatter = new NSNumberFormatter(); // specify just positive format numberFormatter.setPattern("$#,##0.00"); // specify positive and negative formats numberFormatter.setPattern("$#,##0.00;($#,##0.00)"); // specify positive, zero, and negative formats numberFormatter.setFormat("$#,###.00;0.00;($#,##0.00)");
See Also: pattern
public void setPositiveFormat(String format)
public void setPositivePattern(String pattern)
validatePattern()
which throws an IllegalArgumentException if the pattern is null, the pattern string is empty, or the string does not contain one of the characters in ",._#0123456789".
See Also: setPattern
public void setRoundingBehavior(int newRoundingBehavior)
public void setStringForNotANumber(String newString)
public void setStringForNull(String newString)
public void setStringForZero(String aString)
public void setThousandSeparator(String newSeparator)
public String stringForNotANumber()
public String stringForNull()
public String stringForObjectValue(Object object) throws IllegalArgumentException
See Also: setPattern
public String stringForZero()
public String thousandSeparator()
,
). Note that the return value doesn't indicate whether thousand separators are enabled.
© 2001 Apple Computer, Inc. (Last Published April 17, 2001)