|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.apache.velocity.tools.generic.SafeConfig
org.apache.velocity.tools.generic.LocaleConfig
org.apache.velocity.tools.generic.FormatConfig
org.apache.velocity.tools.generic.NumberTool
@DefaultKey(value="number") public class 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($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.
| Field Summary | |
|---|---|
static java.lang.String |
DEFAULT_FORMAT_KEY
Deprecated. |
static java.lang.String |
DEFAULT_LOCALE_KEY
Deprecated. |
| Fields inherited from class org.apache.velocity.tools.generic.FormatConfig |
|---|
DEFAULT_FORMAT, FORMAT_KEY |
| Fields inherited from class org.apache.velocity.tools.generic.LocaleConfig |
|---|
DEFAULT_LOCALE |
| Fields inherited from class org.apache.velocity.tools.generic.SafeConfig |
|---|
LOCK_CONFIG_KEY, OLD_LOCK_CONFIG_KEY, SAFE_MODE_KEY |
| Constructor Summary | |
|---|---|
NumberTool()
|
|
| Method Summary | |
|---|---|
java.lang.String |
currency(java.lang.Object obj)
Convenience method equivalent to $number.format("currency", $foo). |
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 FormatConfig.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 LocaleConfig.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. |
protected java.text.NumberFormat |
getNumberFormat(int numberStyle,
java.util.Locale locale)
Deprecated. |
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)
Deprecated. |
java.lang.String |
integer(java.lang.Object obj)
Convenience method equivalent to $number.format("integer", $foo). |
java.lang.String |
number(java.lang.Object obj)
Convenience method equivalent to $number.format("number", $foo). |
java.lang.String |
percent(java.lang.Object obj)
Convenience method equivalent to $number.format("percent", $foo). |
java.lang.Number |
toNumber(java.lang.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. |
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
LocaleConfig.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 org.apache.velocity.tools.generic.FormatConfig |
|---|
configure, getFormat, setFormat |
| Methods inherited from class org.apache.velocity.tools.generic.LocaleConfig |
|---|
getLocale, setLocale |
| Methods inherited from class org.apache.velocity.tools.generic.SafeConfig |
|---|
configure, isConfigLocked, isSafeMode, setLockConfig, setSafeMode |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
@Deprecated public static final java.lang.String DEFAULT_FORMAT_KEY
@Deprecated public static final java.lang.String DEFAULT_LOCALE_KEY
| Constructor Detail |
|---|
public NumberTool()
| Method Detail |
|---|
public java.lang.String format(java.lang.Object obj)
FormatConfig.getFormat().
obj - the number object to be formatted
format(String format, Object obj, Locale locale)public java.lang.String currency(java.lang.Object obj)
public java.lang.String integer(java.lang.Object obj)
public java.lang.String number(java.lang.Object obj)
public java.lang.String percent(java.lang.Object obj)
public java.lang.String format(java.lang.String format,
java.lang.Object obj)
LocaleConfig.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
@Deprecated
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
parameters@Deprecated 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 FormatConfig.getFormat() and the Locale
returned by LocaleConfig.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
LocaleConfig.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, java.text.ParsePosition)
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||