类 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
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.
- 从以下版本开始:
- VelocityTools 1.2
- 版本:
- $Id: NumberTool.java 671008 2008-06-24 03:37:33Z nbubna $
- 作者:
- Nathan Bubna, Mike Kienenberger
-
字段概要
字段从类继承的字段 org.apache.velocity.tools.generic.FormatConfig
DEFAULT_FORMAT, FORMAT_KEY
从类继承的字段 org.apache.velocity.tools.generic.LocaleConfig
DEFAULT_LOCALE
从类继承的字段 org.apache.velocity.tools.generic.SafeConfig
LOCK_CONFIG_KEY, OLD_LOCK_CONFIG_KEY, SAFE_MODE_KEY
-
构造器概要
构造器 -
方法概要
修饰符和类型方法说明Convenience 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
.protected NumberFormat
getNumberFormat
(int numberStyle, Locale locale) 已过时。getNumberFormat
(String format, Locale locale) Returns aNumberFormat
instance for the specified format andLocale
.protected int
getStyleAsInt
(String style) 已过时。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 ofNumber
using the format returned byFormatConfig.getFormat()
and theLocale
returned byLocaleConfig.getLocale()
if the object is not already an instance of Number.Converts an object to an instance ofNumber
using the specified format and theLocale
returned byLocaleConfig.getLocale()
if the object is not already an instance of Number.从类继承的方法 org.apache.velocity.tools.generic.FormatConfig
configure, getFormat, setFormat
从类继承的方法 org.apache.velocity.tools.generic.LocaleConfig
getLocale, setLocale
从类继承的方法 org.apache.velocity.tools.generic.SafeConfig
configure, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
-
字段详细资料
-
构造器详细资料
-
NumberTool
public NumberTool()
-
-
方法详细资料
-
format
Converts the specified object to a number and formats it according to the pattern or style returned byFormatConfig.getFormat()
.- 参数:
obj
- the number object to be formatted- 返回:
- the specified number formatted as a string
- 另请参阅:
-
currency
Convenience method equivalent to $number.format("currency", $foo).- 从以下版本开始:
- VelocityTools 1.3
-
integer
Convenience method equivalent to $number.format("integer", $foo).- 从以下版本开始:
- VelocityTools 1.3
-
number
Convenience method equivalent to $number.format("number", $foo).- 从以下版本开始:
- VelocityTools 1.3
-
percent
Convenience method equivalent to $number.format("percent", $foo).- 从以下版本开始:
- VelocityTools 1.3
-
format
Converts the specified object to a number and returns a formatted string representing that number in the locale returned byLocaleConfig.getLocale()
.- 参数:
format
- the formatting instructionsobj
- the number object to be formatted- 返回:
- a formatted string for this locale representing the specified
number or
null
if the parameters are invalid - 另请参阅:
-
format
Converts the specified object to a number and returns a formatted string representing that number in the specifiedLocale
.- 参数:
format
- the custom or standard pattern to be usedobj
- the number object to be formattedlocale
- theLocale
to be used when formatting- 返回:
- a formatted string representing the specified number or
null
if the parameters are invalid
-
getNumberFormat
Returns aNumberFormat
instance 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 customizedNumberFormat
will be returned.- 参数:
format
- the custom or standard formatting pattern to be usedlocale
- theLocale
to be used- 返回:
- an instance of
NumberFormat
- 另请参阅:
-
getNumberFormat
已过时。Returns aNumberFormat
instance for the specified number style andLocale
.- 参数:
numberStyle
- the number style (number will be ignored if this is less than zero or the number style is not recognized)locale
- theLocale
to be used- 返回:
- an instance of
NumberFormat
ornull
if an instance cannot be constructed with the given parameters
-
getStyleAsInt
已过时。Checks a string to see if it matches one of the standard NumberFormat style patterns: NUMBER, CURRENCY, PERCENT, INTEGER, or DEFAULT. if it does it will return the integer constant for that pattern. if not, it will return -1.- 参数:
style
- the string to be checked- 返回:
- the int identifying the style pattern
- 另请参阅:
-
toNumber
Converts an object to an instance ofNumber
using the format returned byFormatConfig.getFormat()
and theLocale
returned byLocaleConfig.getLocale()
if the object is not already an instance of Number.- 参数:
obj
- the number to convert- 返回:
- the object as a
Number
ornull
if no conversion is possible
-
toNumber
Converts an object to an instance ofNumber
using the specified format and theLocale
returned byLocaleConfig.getLocale()
if the object is not already an instance of Number.- 参数:
format
- - the format the number is inobj
- - the number to convert- 返回:
- the object as a
Number
ornull
if no conversion is possible - 另请参阅:
-
toNumber
Converts an object to an instance ofNumber
using the specified format andLocale
if the object is not already an instance of Number.
-