|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.apache.velocity.tools.generic.NumberTool
Tool for working with Number in Velocity templates.
It is useful for accessing and
formatting arbitrary Number objects. Also
the tool can be used to retrieve NumberFormat instances
or make conversions to and from various number types.
Example uses:
$myNumber -> 13.55
$number.format('currency',$myNumber) -> $13.55
$number.format('integer',$myNumber) -> 13
Example toolbox.xml config (if you want to use this with VelocityView):
<tool>
<key>number</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.NumberTool</class>
</tool>
This tool is entirely threadsafe, and has no instance members. It may be used in any scope (request, session, or application). As such, the methods are highly interconnected, and overriding key methods provides an easy way to create subclasses that use a non-default format or locale.
| Field Summary | |
static java.lang.String |
DEFAULT_FORMAT
The default format to be used when none is specified. |
private static int |
STYLE_CURRENCY
|
private static int |
STYLE_INTEGER
|
private static int |
STYLE_NUMBER
|
private static int |
STYLE_PERCENT
|
| Constructor Summary | |
NumberTool()
Default constructor. |
|
| Method Summary | |
java.lang.String |
format(java.lang.Object obj)
Converts the specified object to a number and formats it according to the pattern or style returned by getFormat(). |
java.lang.String |
format(java.lang.String format,
java.lang.Object obj)
Converts the specified object to a number and returns a formatted string representing that number in the locale returned by getLocale(). |
java.lang.String |
format(java.lang.String format,
java.lang.Object obj,
java.util.Locale locale)
Converts the specified object to a number and returns a formatted string representing that number in the specified Locale. |
java.lang.String |
getFormat()
Return the pattern or style to be used for formatting numbers when none is specified. |
private java.text.NumberFormat |
getIntegerInstance(java.util.Locale locale)
Since we wish to continue supporting Java 1.3, for the present we cannot use Java 1.4's NumberFormat.getIntegerInstance(Locale) method. |
java.util.Locale |
getLocale()
This implementation returns the default locale. |
protected java.text.NumberFormat |
getNumberFormat(int numberStyle,
java.util.Locale locale)
Returns a NumberFormat instance for the specified
number style and Locale. |
java.text.NumberFormat |
getNumberFormat(java.lang.String format,
java.util.Locale locale)
Returns a NumberFormat instance for the specified
format and Locale. |
protected int |
getStyleAsInt(java.lang.String style)
Checks a string to see if it matches one of the standard NumberFormat style patterns: NUMBER, CURRENCY, PERCENT, INTEGER, or DEFAULT. |
java.lang.Number |
toNumber(java.lang.Object obj)
Converts an object to an instance of Number using the
format returned by getFormat() and the Locale
returned by getLocale() if the object is not already
an instance of Number. |
java.lang.Number |
toNumber(java.lang.String format,
java.lang.Object obj)
Converts an object to an instance of Number using the
specified format and the Locale returned by
getLocale() if the object is not already an instance
of Number. |
java.lang.Number |
toNumber(java.lang.String format,
java.lang.Object obj,
java.util.Locale locale)
Converts an object to an instance of Number using the
specified format and Localeif the object is not already
an instance of Number. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
public static final java.lang.String DEFAULT_FORMAT
private static final int STYLE_NUMBER
private static final int STYLE_CURRENCY
private static final int STYLE_PERCENT
private static final int STYLE_INTEGER
| Constructor Detail |
public NumberTool()
| Method Detail |
public java.util.Locale getLocale()
Localepublic java.lang.String getFormat()
NOTE: At some point in the future it may be feasible to configure this value via the toolbox definition, but at present, it is not possible to specify custom tool configurations there. For now you should just override this in a subclass to have a different default.
public java.lang.String format(java.lang.Object obj)
getFormat().
obj - the number object to be formatted
format(String format, Object obj, Locale locale)
public java.lang.String format(java.lang.String format,
java.lang.Object obj)
getLocale().
format - the formatting instructionsobj - the number object to be formatted
null if the parameters are invalidformat(String format, Object obj, Locale locale)
public java.lang.String format(java.lang.String format,
java.lang.Object obj,
java.util.Locale locale)
Locale.
format - the custom or standard pattern to be usedobj - the number object to be formattedlocale - the Locale to be used when formatting
null if the parameters are invalid
public java.text.NumberFormat getNumberFormat(java.lang.String format,
java.util.Locale locale)
NumberFormat instance for the specified
format and Locale. If the format specified is a standard
style pattern, then a number instance
will be returned with the number style set to the
specified style. If it is a custom format, then a customized
NumberFormat will be returned.
format - the custom or standard formatting pattern to be usedlocale - the Locale to be used
NumberFormatNumberFormat
protected java.text.NumberFormat getNumberFormat(int numberStyle,
java.util.Locale locale)
NumberFormat instance for the specified
number style and Locale.
numberStyle - the number style (number will be ignored if this is
less than zero or the number style is not recognized)locale - the Locale to be used
NumberFormat or null
if an instance cannot be constructed with the given
parametersprivate java.text.NumberFormat getIntegerInstance(java.util.Locale locale)
protected int getStyleAsInt(java.lang.String style)
style - the string to be checked
NumberFormatpublic java.lang.Number toNumber(java.lang.Object obj)
Number using the
format returned by getFormat() and the Locale
returned by getLocale() if the object is not already
an instance of Number.
obj - the number to convert
Number or null if no
conversion is possible
public java.lang.Number toNumber(java.lang.String format,
java.lang.Object obj)
Number using the
specified format and the Locale returned by
getLocale() if the object is not already an instance
of Number.
format - - the format the number is inobj - - the number to convert
Number or null if no
conversion is possibletoNumber(String format, Object obj, Locale locale)
public java.lang.Number toNumber(java.lang.String format,
java.lang.Object obj,
java.util.Locale locale)
Number using the
specified format and Localeif the object is not already
an instance of Number.
format - - the format the number is inobj - - the number to convertlocale - - the Locale
Number or null if no
conversion is possibleNumberFormat.parse(java.lang.String)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||