类 MathTool
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.MathTool
- 所有已实现的接口:
Serializable
@DefaultKey("math")
@ValidScope("application")
public class MathTool
extends FormatConfig
implements Serializable
Tool for performing math in Velocity.
Some things should be noted here:
- This class does not have methods that take primitives. This is simply because Velocity wraps all primitives for us automagically.
- No null pointer, number format, or divide by zero exceptions are thrown here. This is because such exceptions thrown in template halt rendering. It should be sufficient debugging feedback that Velocity will render the reference literally. (e.g. $math.div(1, 0) renders as '$math.div(1, 0)')
Example tools.xml config:
<tools>
<toolbox scope="application">
<tool class="org.apache.velocity.tools.generic.MathTool"/>
</toolbox>
</tools>
- 版本:
- $Revision$ $Date$
- 作者:
- Nathan Bubna, Leon Messerschmidt
- 另请参阅:
-
字段概要
从类继承的字段 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, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明bitwiseAnd(Object... nums) Bitwise AndbitwiseAnd(Object num1, Object num2) Bitwise OrbitwiseXOr(Object... nums) Bitwise XOrbitwiseXOr(Object num1, Object num2) getAverage(double... values) Get the average of the values in an array of double valuesgetAverage(long... values) Get the average of the values in an array of long valuesgetAverage(Object... array) Get the average of the valuesgetAverage(Object[] array, String field) Get the sum of the values from a listgetAverage(Collection collection) Get the average of the valuesgetAverage(Collection collection, String field) Get the average of the values from a listgetTotal(double... values) Get the sum of the valuesgetTotal(long... values) Get the sum of the valuesGet the sum of the valuesGet the sum of the values from a listgetTotal(Collection collection) Get the sum of the valuesgetTotal(Collection collection, String field) Get the sum of the values from a listprotected booleanhasFloatingPoint(String value) Does integer division on the int values of the specified numbers.protected NumberTakes the original argument(s) and returns the resulting value as an instance of the best matching type (Integer, Long, or Double).protected Numberprotected NumberDoes integer modulus on the int values of the specified numbers.This returns a randomNumberwithin the specified range.Rounds a number to the nearest whole IntegerRounds a number to the specified number of decimal places.已过时。已过时。useNumberTool.toNumber(Object).intValue()已过时。useNumberTool.toNumber(Object).longValue()已过时。从类继承的方法 org.apache.velocity.tools.generic.FormatConfig
configure, getFormat, setFormat从类继承的方法 org.apache.velocity.tools.generic.LocaleConfig
getLocale, setLocale, toLocale从类继承的方法 org.apache.velocity.tools.generic.SafeConfig
configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
-
构造器详细资料
-
MathTool
public MathTool()
-
-
方法详细资料
-
add
-
sub
-
mul
-
div
-
max
-
min
-
bitwiseOr
-
bitwiseAnd
-
bitwiseXOr
-
add
- 参数:
nums- the numbers to be added- 返回:
- the sum of the numbers or
nullif they're invalid - 另请参阅:
-
sub
- 参数:
nums- the numbers to be subtracted- 返回:
- the difference of the numbers (subtracted in order) or
nullif they're invalid - 另请参阅:
-
mul
- 参数:
nums- the numbers to be multiplied- 返回:
- the product of the numbers or
nullif they're invalid - 另请参阅:
-
div
- 参数:
nums- the numbers to be divided- 返回:
- the quotient of the numbers or
nullif they're invalid or if any denominator equals zero - 另请参阅:
-
pow
- 参数:
num1- the first numbernum2- the second number- 返回:
- the first number raised to the power of the
second or
nullif they're invalid - 另请参阅:
-
idiv
Does integer division on the int values of the specified numbers.So, $math.idiv('5.1',3) will return '1', and $math.idiv(6,'3.9') will return '2'.
- 参数:
num1- the first numbernum2- the second number- 返回:
- the result of performing integer division on the operands.
- 另请参阅:
-
mod
Does integer modulus on the int values of the specified numbers.So, $math.mod('5.1',3) will return '2', and $math.mod(6,'3.9') will return '0'.
- 参数:
num1- the first numbernum2- the second number- 返回:
- the result of performing integer modulus on the operands.
- 另请参阅:
-
bitwiseOr
Bitwise Or- 参数:
nums- the numbers to be Or'ed- 返回:
- the bitwise Or of the numbers or
nullif they're invalid - 另请参阅:
-
bitwiseAnd
Bitwise And- 参数:
nums- the numbers to be And'ed- 返回:
- the bitwise And of the numbers or
nullif they're invalid - 另请参阅:
-
bitwiseXOr
Bitwise XOr- 参数:
nums- the numbers to be XOr'ed- 返回:
- the bitwise XOr of the numbers or
nullif they're invalid - 另请参阅:
-
max
- 参数:
nums- the numbers to be searched- 返回:
- the largest of the numbers or
nullif they're invalid - 另请参阅:
-
min
- 参数:
nums- the numbers to be searched- 返回:
- the smallest of the numbers or
nullif they're invalid - 另请参阅:
-
abs
- 参数:
num- the number- 返回:
- the absolute value of the number or
nullif it's invalid - 另请参阅:
-
ceil
- 参数:
num- the number- 返回:
- the smallest integer that is not less than the given number
-
floor
- 参数:
num- the number- 返回:
- the integer portion of the number
-
round
Rounds a number to the nearest whole Integer- 参数:
num- the number to round- 返回:
- the number rounded to the nearest whole Integer
or
nullif it's invalid - 另请参阅:
-
roundTo
Rounds a number to the specified number of decimal places. This is particulary useful for simple display formatting. If you want to round an number to the nearest integer, it is better to useround(java.lang.Object), as that will return anIntegerrather than aDouble.- 参数:
decimals- the number of decimal placesnum- the number to round- 返回:
- the value rounded to the specified number of
decimal places or
nullif it's invalid - 另请参阅:
-
getRandom
- 返回:
- a pseudo-random
Doublegreater than or equal to 0.0 and less than 1.0 - 另请参阅:
-
random
This returns a randomNumberwithin the specified range. The returned value will be greater than or equal to the first number and less than the second number. If both arguments are whole numbers then the returned number will also be, otherwise aDoublewill be returned.- 参数:
num1- the first numbernum2- the second number- 返回:
- a pseudo-random
Numbergreater than or equal to the first number and less than the second - 另请参阅:
-
toInteger
已过时。useNumberTool.toNumber(Object).intValue()Converts an object with a numeric value into an Integer Valid formats areNumberor aStringrepresentation of a number- 参数:
num- the number to be converted- 返回:
- a
Integerrepresentation of the number ornullif it's invalid
-
toLong
已过时。useNumberTool.toNumber(Object).longValue()Converts an object with a numeric value into a Long integer Valid formats areNumberor aStringrepresentation of a number- 参数:
num- the number to be converted- 返回:
- a
Longinteger representation of the number ornullif it's invalid
-
toDouble
已过时。useNumberTool.toNumber(Object).doubleValue()Converts an object with a numeric value into a Double Valid formats areNumberor aStringrepresentation of a number- 参数:
num- the number to be converted- 返回:
- a
Doublerepresentation of the number ornullif it's invalid
-
toNumber
已过时。Converts an object with a numeric value into a Number Valid formats areNumberor aStringrepresentation of a number. Note that this does not handle localized number formats. Use theNumberToolto handle such conversions.- 参数:
num- the number to be converted- 返回:
- a
Numberrepresentation of the number ornullif it's invalid
-
matchType
- 参数:
in- instance of wanted Number classout- input number- 返回:
- wanted Number
- 另请参阅:
-
matchType
- 参数:
in1- instance #1 of wanted Number classin2- instance #1 of wanted Number classout- input number- 返回:
- wanted Number
- 另请参阅:
-
matchType
Takes the original argument(s) and returns the resulting value as an instance of the best matching type (Integer, Long, or Double). If either an argument or the result is not an integer (i.e. has no decimal when rendered) the result will be returned as a Double. If not and the result is < -2147483648 or > 2147483647, then a Long will be returned. Otherwise, an Integer will be returned.- 参数:
out- target number valuein- wanted Number classes- 返回:
- wanted Number
-
hasFloatingPoint
- 参数:
value- target value- 返回:
- wether it contains a decimal separator (non locale-aware for now - TODO)
-
getTotal
Get the sum of the values from a list- 参数:
collection- A collection containing Java beansfield- A Java Bean field for the objects in collection that will return a number.- 返回:
- The sum of the values in collection.
-
getAverage
Get the average of the values from a list- 参数:
collection- A collection containing Java beansfield- A Java Bean field for the objects in collection that will return a number.- 返回:
- The average of the values in collection.
-
getTotal
Get the sum of the values from a list- 参数:
array- An array containing Java beansfield- A Java Bean field for the objects in array that will return a number.- 返回:
- The sum of the values in array.
-
getAverage
Get the sum of the values from a list- 参数:
array- A collection containing Java beansfield- A Java Bean field for the objects in array that will return a number.- 返回:
- The sum of the values in array.
-
getTotal
Get the sum of the values- 参数:
collection- A collection containing numeric values- 返回:
- The sum of the values in collection.
-
getAverage
Get the average of the values- 参数:
collection- A collection containing number values- 返回:
- The average of the values in collection.
-
getTotal
Get the sum of the values- 参数:
array- An array containing number values- 返回:
- The sum of the values in array.
-
getAverage
Get the average of the values- 参数:
array- An array containing number values- 返回:
- The sum of the values in array.
-
getTotal
Get the sum of the values- 参数:
values- The list of double values to add up.- 返回:
- The sum of the arrays
-
getAverage
Get the average of the values in an array of double values- 参数:
values- The list of double values- 返回:
- The average of the array of values
-
getTotal
Get the sum of the values- 参数:
values- The list of long values to add up.- 返回:
- The sum of the arrays
-
getAverage
Get the average of the values in an array of long values- 参数:
values- The list of long values- 返回:
- The average of the array of values
-
NumberTool.toNumber(Object).doubleValue()