@DefaultKey(value="number") public class NumberTool extends FormatConfig implements Serializable
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 -gt; 13.55 $number.format($myNumber) -gt; 13.6 $number.currency($myNumber) -gt; $13.55 $number.integer($myNumber) -gt; 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.
DEFAULT_FORMAT, FORMAT_KEY
DEFAULT_LOCALE
LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_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 . |
NumberFormat |
getNumberFormat(String format,
Locale locale)
Returns a
NumberFormat instance for the specified
format and Locale . |
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, toLocale
configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
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)
obj
- the number object to be formattedpublic String integer(Object obj)
obj
- the number object to be formattedpublic String number(Object obj)
obj
- the number object to be formattedpublic String percent(Object obj)
obj
- the number object to be formattedpublic 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
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–2018 The Apache Software Foundation. All rights reserved.