@DefaultKey(value="number") public class NumberTool extends FormatConfig
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($myNumber) -> 13.6 $number.currency($myNumber) -> $13.55 $number.integer($myNumber) -> 13 Example tools.xml config (if you want to use this with VelocityView): <tools> <toolbox scope="application"> <tool class="org.apache.velocity.tools.generic.MathTool"/> </toolbox> </tools>
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_KEY
Deprecated.
|
static String |
DEFAULT_LOCALE_KEY
Deprecated.
|
DEFAULT_FORMAT, FORMAT_KEY
DEFAULT_LOCALE
LOCK_CONFIG_KEY, OLD_LOCK_CONFIG_KEY, SAFE_MODE_KEY
Constructor and Description |
---|
NumberTool() |
Modifier and Type | Method and Description |
---|---|
String |
currency(Object obj)
Convenience method equivalent to $number.format("currency", $foo).
|
String |
format(Object obj)
Converts the specified object to a number and formats it according to
the pattern or style returned by
FormatConfig.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
LocaleConfig.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 . |
protected NumberFormat |
getNumberFormat(int numberStyle,
Locale locale)
Deprecated.
|
NumberFormat |
getNumberFormat(String format,
Locale locale)
Returns a
NumberFormat instance for the specified
format and Locale . |
protected int |
getStyleAsInt(String style)
Deprecated.
|
String |
integer(Object obj)
Convenience method equivalent to $number.format("integer", $foo).
|
String |
number(Object obj)
Convenience method equivalent to $number.format("number", $foo).
|
String |
percent(Object obj)
Convenience method equivalent to $number.format("percent", $foo).
|
Number |
toNumber(Object obj)
Converts an object to an instance of
Number using the
format returned by FormatConfig.getFormat() and the Locale
returned by LocaleConfig.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
LocaleConfig.getLocale() if the object is not already an instance
of Number. |
Number |
toNumber(String format,
Object obj,
Locale locale)
|
configure, getFormat, setFormat
getLocale, setLocale
configure, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
@Deprecated public static final String DEFAULT_FORMAT_KEY
@Deprecated public static final String DEFAULT_LOCALE_KEY
public String format(Object obj)
FormatConfig.getFormat()
.obj
- the number object to be formattedformat(String format, Object obj, Locale locale)
public String currency(Object obj)
public String integer(Object obj)
public String number(Object obj)
public String percent(Object obj)
public String format(String format, Object obj)
LocaleConfig.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
@Deprecated 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
parameters@Deprecated protected int getStyleAsInt(String style)
style
- the string to be checkedNumberFormat
public Number toNumber(Object obj)
Number
using the
format returned by FormatConfig.getFormat()
and the Locale
returned by LocaleConfig.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
LocaleConfig.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–2014 Apache Software Foundation. All rights reserved.