public class NumberTool extends Object
If we need more tools in future, we should look at importing this instead
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) -> 13Example 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.
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_FORMAT
The default format to be used when none is specified.
|
Constructor and Description |
---|
NumberTool()
Default constructor.
|
NumberTool(Locale locale) |
Modifier and Type | Method and Description |
---|---|
String |
format(Object obj)
Converts the specified object to a number and formats it according to
the pattern or style returned by
getFormat() . |
String |
format(String format,
Object obj)
Converts the specified object to a number and returns
a formatted string representing that number in the locale
returned by
getLocale() . |
String |
format(String format,
Object obj,
Locale locale)
Converts the specified object to a number and returns
a formatted string representing that number in the specified
Locale . |
String |
getFormat()
Return the pattern or style to be used for formatting numbers when none
is specified.
|
Locale |
getLocale()
This implementation returns the default locale.
|
protected NumberFormat |
getNumberFormat(int numberStyle,
Locale locale)
Returns a
NumberFormat instance for the specified
number style and Locale . |
NumberFormat |
getNumberFormat(String format,
Locale locale)
Returns a
NumberFormat instance for the specified
format and Locale . |
protected int |
getStyleAsInt(String style)
Checks a string to see if it matches one of the standard
NumberFormat style patterns:
NUMBER, CURRENCY, PERCENT, INTEGER, or DEFAULT.
|
Number |
toNumber(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. |
Number |
toNumber(String format,
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. |
Number |
toNumber(String format,
Object obj,
Locale locale)
|
public static final String DEFAULT_FORMAT
public NumberTool()
public NumberTool(Locale locale)
public Locale getLocale()
Locale
public 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 String format(Object obj)
getFormat()
.obj
- the number object to be formattedformat(String format, Object obj, Locale locale)
public String format(String format, Object obj)
getLocale()
.format
- the formatting instructionsobj
- the number object to be formattednull
if the parameters are invalidformat(String format, Object obj, Locale locale)
public String format(String format, Object obj, Locale locale)
Locale
.format
- the custom or standard pattern to be usedobj
- the number object to be formattedlocale
- the Locale
to be used when formattingnull
if the parameters are invalidpublic NumberFormat getNumberFormat(String format, 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 usedNumberFormat
NumberFormat
protected NumberFormat getNumberFormat(int numberStyle, 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 usedNumberFormat
or null
if an instance cannot be constructed with the given
parametersprotected int getStyleAsInt(String style)
style
- the string to be checkedNumberFormat
public Number toNumber(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 convertNumber
or null
if no
conversion is possiblepublic Number toNumber(String format, 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 convertNumber
or null
if no
conversion is possibletoNumber(String format, Object obj, Locale locale)
public Number toNumber(String format, Object obj, Locale locale)
Number
using the
specified format and Locale
if 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, java.text.ParsePosition)
Copyright © 2002-2023 Atlassian. All Rights Reserved.