Class 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
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>
- Version:
- $Revision: 696463 $ $Date: 2008-09-17 14:39:04 -0700 (Wed, 17 Sep 2008) $
- Author:
- Nathan Bubna, Leon Messerschmidt
-
Field Summary
Fields inherited from class org.apache.velocity.tools.generic.FormatConfig
DEFAULT_FORMAT, FORMAT_KEYFields inherited from class org.apache.velocity.tools.generic.LocaleConfig
DEFAULT_LOCALEFields inherited from class org.apache.velocity.tools.generic.SafeConfig
LOCK_CONFIG_KEY, OLD_LOCK_CONFIG_KEY, SAFE_MODE_KEY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetAverage(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.protected NumberparseNumber(String value) Deprecated.This returns a randomNumberwithin the specified range.Rounds a number to the nearest whole IntegerRounds a number to the specified number of decimal places.Methods inherited from class org.apache.velocity.tools.generic.FormatConfig
configure, getFormat, setFormatMethods inherited from class org.apache.velocity.tools.generic.LocaleConfig
getLocale, setLocaleMethods inherited from class org.apache.velocity.tools.generic.SafeConfig
configure, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
-
Constructor Details
-
MathTool
public MathTool()
-
-
Method Details
-
add
-
sub
-
mul
-
div
-
max
-
min
-
add
- Parameters:
nums- the numbers to be added- Returns:
- the sum of the numbers or
nullif they're invalid - See Also:
-
sub
- Parameters:
nums- the numbers to be subtracted- Returns:
- the difference of the numbers (subtracted in order) or
nullif they're invalid - See Also:
-
mul
- Parameters:
nums- the numbers to be multiplied- Returns:
- the product of the numbers or
nullif they're invalid - See Also:
-
div
- Parameters:
nums- the numbers to be divided- Returns:
- the quotient of the numbers or
nullif they're invalid or if any denominator equals zero - See Also:
-
pow
- Parameters:
num1- the first numbernum2- the second number- Returns:
- the first number raised to the power of the
second or
nullif they're invalid - See Also:
-
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'.
- Parameters:
num1- the first numbernum2- the second number- Returns:
- the result of performing integer division on the operands.
- See Also:
-
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'.
- Parameters:
num1- the first numbernum2- the second number- Returns:
- the result of performing integer modulus on the operands.
- See Also:
-
max
- Parameters:
nums- the numbers to be searched- Returns:
- the largest of the numbers or
nullif they're invalid - See Also:
-
min
- Parameters:
nums- the numbers to be searched- Returns:
- the smallest of the numbers or
nullif they're invalid - See Also:
-
abs
- Parameters:
num- the number- Returns:
- the absolute value of the number or
nullif it's invalid - See Also:
-
ceil
- Parameters:
num- the number- Returns:
- the smallest integer that is not less than the given number
-
floor
- Parameters:
num- the number- Returns:
- the integer portion of the number
-
round
Rounds a number to the nearest whole Integer- Parameters:
num- the number to round- Returns:
- the number rounded to the nearest whole Integer
or
nullif it's invalid - See Also:
-
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.- Parameters:
decimals- the number of decimal placesnum- the number to round- Returns:
- the value rounded to the specified number of
decimal places or
nullif it's invalid - See Also:
-
getRandom
- Returns:
- a pseudo-random
Doublegreater than or equal to 0.0 and less than 1.0 - See Also:
-
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.- Parameters:
num1- the first numbernum2- the second number- Returns:
- a pseudo-random
Numbergreater than or equal to the first number and less than the second - See Also:
-
toInteger
Converts an object with a numeric value into an Integer Valid formats areNumberor aStringrepresentation of a number- Parameters:
num- the number to be converted- Returns:
- a
Integerrepresentation of the number ornullif it's invalid
-
toDouble
Converts an object with a numeric value into a Double Valid formats areNumberor aStringrepresentation of a number- Parameters:
num- the number to be converted- Returns:
- 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.- Parameters:
num- the number to be converted- Returns:
- a
Numberrepresentation of the number ornullif it's invalid
-
matchType
- See Also:
-
matchType
- See Also:
-
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. -
hasFloatingPoint
-
parseNumber
Deprecated. -
getTotal
Get the sum of the values from a list- Parameters:
collection- A collection containing Java beansfield- A Java Bean field for the objects in collection that will return a number.- Returns:
- The sum of the values in collection.
-
getAverage
Get the average of the values from a list- Parameters:
collection- A collection containing Java beansfield- A Java Bean field for the objects in collection that will return a number.- Returns:
- The average of the values in collection.
-
getTotal
Get the sum of the values from a list- Parameters:
array- An array containing Java beansfield- A Java Bean field for the objects in array that will return a number.- Returns:
- The sum of the values in array.
-
getAverage
Get the sum of the values from a list- Parameters:
array- A collection containing Java beansfield- A Java Bean field for the objects in array that will return a number.- Returns:
- The sum of the values in array.
-
getTotal
Get the sum of the values- Parameters:
collection- A collection containing numeric values- Returns:
- The sum of the values in collection.
-
getAverage
Get the average of the values- Parameters:
collection- A collection containing number values- Returns:
- The average of the values in collection.
-
getTotal
Get the sum of the values- Parameters:
array- An array containing number values- Returns:
- The sum of the values in array.
-
getAverage
Get the average of the values- Parameters:
array- An array containing number values- Returns:
- The sum of the values in array.
-
getTotal
Get the sum of the values- Parameters:
values- The list of double values to add up.- Returns:
- The sum of the arrays
-
getAverage
Get the average of the values in an array of double values- Parameters:
values- The list of double values- Returns:
- The average of the array of values
-
getTotal
Get the sum of the values- Parameters:
values- The list of long values to add up.- Returns:
- The sum of the arrays
-
getAverage
Get the average of the values in an array of long values- Parameters:
values- The list of long values- Returns:
- The average of the array of values
-