Class NumberTool
java.lang.Object
org.apache.velocity.tools.generic.SafeConfig
org.apache.velocity.tools.generic.LocaleConfig
org.apache.velocity.tools.generic.FormatConfig
org.apache.velocity.tools.generic.NumberTool
- All Implemented Interfaces:
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.
- Since:
- VelocityTools 1.2
- Version:
- $Id$
- Author:
- Nathan Bubna, Mike Kienenberger
- See Also:
-
Field Summary
Fields inherited from class FormatConfig
DEFAULT_FORMAT, FORMAT_KEYFields inherited from class LocaleConfig
DEFAULT_LOCALEFields inherited from class SafeConfig
LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionConvenience method equivalent to $number.format("currency", $foo).Converts the specified object to a number and formats it according to the pattern or style returned byFormatConfig.getFormat().Converts the specified object to a number and returns a formatted string representing that number in the locale returned byLocaleConfig.getLocale().Converts the specified object to a number and returns a formatted string representing that number in the specifiedLocale.getNumberFormat(String format, Locale locale) Returns aNumberFormatinstance for the specified format andLocale.Convenience method equivalent to $number.format("integer", $foo).Convenience method equivalent to $number.format("number", $foo).Convenience method equivalent to $number.format("percent", $foo).Converts an object to an instance ofBooleanif the object is not already an instance of Boolean.Converts an object to an instance ofNumberusing the format returned byFormatConfig.getFormat()and theLocalereturned byLocaleConfig.getLocale()if the object is not already an instance of Number.Converts an object to an instance ofNumberusing the specified format and theLocalereturned byLocaleConfig.getLocale()if the object is not already an instance of Number.Methods inherited from class FormatConfig
configure, getFormat, setFormatMethods inherited from class LocaleConfig
getLocale, setLocale, toLocaleMethods inherited from class SafeConfig
configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
-
Constructor Details
-
NumberTool
public NumberTool()
-
-
Method Details
-
format
Converts the specified object to a number and formats it according to the pattern or style returned byFormatConfig.getFormat().- Parameters:
obj- the number object to be formatted- Returns:
- the specified number formatted as a string
- See Also:
-
currency
-
integer
-
number
-
percent
-
format
Converts the specified object to a number and returns a formatted string representing that number in the locale returned byLocaleConfig.getLocale().- Parameters:
format- the formatting instructionsobj- the number object to be formatted- Returns:
- a formatted string for this locale representing the specified
number or
nullif the parameters are invalid - See Also:
-
format
Converts the specified object to a number and returns a formatted string representing that number in the specifiedLocale.- Parameters:
format- the custom or standard pattern to be usedobj- the number object to be formattedlocale- theLocaleto be used when formatting- Returns:
- a formatted string representing the specified number or
nullif the parameters are invalid
-
getNumberFormat
Returns aNumberFormatinstance for the specified format andLocale. 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 customizedNumberFormatwill be returned.- Parameters:
format- the custom or standard formatting pattern to be usedlocale- theLocaleto be used- Returns:
- an instance of
NumberFormat - See Also:
-
toNumber
Converts an object to an instance ofNumberusing the format returned byFormatConfig.getFormat()and theLocalereturned byLocaleConfig.getLocale()if the object is not already an instance of Number.- Parameters:
obj- the number to convert- Returns:
- the object as a
Numberornullif no conversion is possible
-
toNumber
Converts an object to an instance ofNumberusing the specified format and theLocalereturned byLocaleConfig.getLocale()if the object is not already an instance of Number.- Parameters:
format- - the format the number is inobj- - the number to convert- Returns:
- the object as a
Numberornullif no conversion is possible - See Also:
-
toNumber
-
toBoolean
-