| java.lang.Object org.apache.velocity.tools.generic.MathTool
MathTool | public class MathTool (Code) | | 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 toolbox.xml config (if you want to use this with VelocityView):
<tool>
<key>math</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.MathTool</class>
</tool>
author: Nathan Bubna author: Leon Messerschmidt version: $Revision: 479724 $ $Date: 2006-11-27 10:49:37 -0800 (Mon, 27 Nov 2006) $ |
Method Summary | |
public Number | abs(Object num) | public Number | add(Object num1, Object num2) | public Integer | ceil(Object num) | public Number | div(Object num1, Object num2) | public Integer | floor(Object num) | public Number | getAverage(Collection collection, String field) Get the average of the values from a list
Parameters: collection - A collection containing Java beans Parameters: field - A Java Bean field for the objects in collection thatwill return a number. | public Number | getAverage(Object[] array, String field) Get the sum of the values from a list
Parameters: array - A collection containing Java beans Parameters: field - A Java Bean field for the objects in array thatwill return a number. | public Number | getAverage(Collection collection) | public Number | getAverage(Object[] array) | public Number | getAverage(double[] values) | public Number | getAverage(long[] values) | public Double | getRandom() | public Number | getTotal(Collection collection, String field) Get the sum of the values from a list
Parameters: collection - A collection containing Java beans Parameters: field - A Java Bean field for the objects in collection thatwill return a number. | public Number | getTotal(Object[] array, String field) Get the sum of the values from a list
Parameters: array - An array containing Java beans Parameters: field - A Java Bean field for the objects in array thatwill return a number. | public Number | getTotal(Collection collection) | public Number | getTotal(Object[] array) | public Number | getTotal(double[] values) Get the sum of the values
Parameters: values - The list of double values to add up. | public Number | getTotal(long[] values) Get the sum of the values
Parameters: values - The list of long values to add up. | public Integer | idiv(Object num1, Object num2) Does integer division on the int values of the specified numbers. | protected Number | matchType(Number in, double out) | protected Number | matchType(Number in1, Number in2, double out) 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. | public Number | max(Object num1, Object num2) | public Number | min(Object num1, Object num2) | public Integer | mod(Object num1, Object num2) Does integer modulus on the int values of the specified numbers. | public Number | mul(Object num1, Object num2) | protected Number | parseNumber(String value) Converts an object into a
Number (if it can)
This is used as the base for all numeric parsing methods. | public Number | pow(Object num1, Object num2) | public Number | random(Object num1, Object num2) This returns a random
Number within the
specified range. | public Integer | round(Object num) | public Double | roundTo(Object decimals, Object num) Rounds a number to the specified number of decimal places. | public Number | sub(Object num1, Object num2) | public Double | toDouble(Object num) | public Integer | toInteger(Object num) | public Number | toNumber(Object num) Converts an object with a numeric value into a Number
Valid formats are
Number or a
String representation of a number. |
ceil | public Integer ceil(Object num)(Code) | | Parameters: num - the number the smallest integer that is notless than the given number |
floor | public Integer floor(Object num)(Code) | | Parameters: num - the number the integer portion of the number |
getAverage | public Number getAverage(Collection collection, String field)(Code) | | Get the average of the values from a list
Parameters: collection - A collection containing Java beans Parameters: field - A Java Bean field for the objects in collection thatwill return a number. The average of the values in collection. |
getAverage | public Number getAverage(Object[] array, String field)(Code) | | Get the sum of the values from a list
Parameters: array - A collection containing Java beans Parameters: field - A Java Bean field for the objects in array thatwill return a number. The sum of the values in array. |
getAverage | public Number getAverage(Collection collection)(Code) | | Get the average of the values
Parameters: collection - A collection containing number values The average of the values in collection. |
getAverage | public Number getAverage(Object[] array)(Code) | | Get the average of the values
Parameters: array - An array containing number values The sum of the values in array. |
getAverage | public Number getAverage(double[] values)(Code) | | Get the average of the values in an array of double values
Parameters: values - The list of double values The average of the array of values |
getAverage | public Number getAverage(long[] values)(Code) | | Get the average of the values in an array of long values
Parameters: values - The list of long values The average of the array of values |
getTotal | public Number getTotal(Collection collection, String field)(Code) | | Get the sum of the values from a list
Parameters: collection - A collection containing Java beans Parameters: field - A Java Bean field for the objects in collection thatwill return a number. The sum of the values in collection. |
getTotal | public Number getTotal(Object[] array, String field)(Code) | | Get the sum of the values from a list
Parameters: array - An array containing Java beans Parameters: field - A Java Bean field for the objects in array thatwill return a number. The sum of the values in array. |
getTotal | public Number getTotal(Collection collection)(Code) | | Get the sum of the values
Parameters: collection - A collection containing numeric values The sum of the values in collection. |
getTotal | public Number getTotal(Object[] array)(Code) | | Get the sum of the values
Parameters: array - An array containing number values The sum of the values in array. |
getTotal | public Number getTotal(double[] values)(Code) | | Get the sum of the values
Parameters: values - The list of double values to add up. The sum of the arrays |
getTotal | public Number getTotal(long[] values)(Code) | | Get the sum of the values
Parameters: values - The list of long values to add up. The sum of the arrays |
idiv | public Integer idiv(Object num1, Object num2)(Code) | | 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 number Parameters: num2 - the second number the result of performing integer divisionon the operands. See Also: MathTool.toInteger |
matchType | protected Number matchType(Number in1, Number in2, double out)(Code) | | 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.
|
mod | public Integer mod(Object num1, Object num2)(Code) | | 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 number Parameters: num2 - the second number the result of performing integer moduluson the operands. See Also: MathTool.toInteger |
parseNumber | protected Number parseNumber(String value) throws NumberFormatException(Code) | | Converts an object into a
Number (if it can)
This is used as the base for all numeric parsing methods. So,
sub-classes can override to allow for customized number parsing.
(e.g. for i18n, fractions, compound numbers, bigger numbers, etc.)
Parameters: value - the string to be parsed the value as a Number |
pow | public Number pow(Object num1, Object num2)(Code) | | Parameters: num1 - the first number Parameters: num2 - the second number the first number raised to the power of thesecond or null if they're invalid See Also: MathTool.toNumber |
random | public Number random(Object num1, Object num2)(Code) | | This returns a random
Number within 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 a
Double will
be returned.
Parameters: num1 - the first number Parameters: num2 - the second number a pseudo-random Number greater thanor equal to the first number and less thanthe second See Also: Math.random |
round | public Integer round(Object num)(Code) | | Rounds a number to the nearest whole Integer
Parameters: num - the number to round the number rounded to the nearest whole Integeror null if it's invalid See Also: java.lang.Math.rint(double) |
roundTo | public Double roundTo(Object decimals, Object num)(Code) | | 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 use
MathTool.round , as that will return
an
Integer rather than a
Double .
Parameters: decimals - the number of decimal places Parameters: num - the number to round the value rounded to the specified number ofdecimal places or null if it's invalid See Also: MathTool.toNumber |
sub | public Number sub(Object num1, Object num2)(Code) | | Parameters: num1 - the first number Parameters: num2 - the second number the difference of the numbers ornull if they're invalid See Also: MathTool.toNumber |
toDouble | public Double toDouble(Object num)(Code) | | Converts an object with a numeric value into a Double
Valid formats are
Number or a
String representation of a number
Parameters: num - the number to be converted a Double representation of the numberor null if it's invalid |
toInteger | public Integer toInteger(Object num)(Code) | | Converts an object with a numeric value into an Integer
Valid formats are
Number or a
String representation of a number
Parameters: num - the number to be converted a Integer representation of the numberor null if it's invalid |
toNumber | public Number toNumber(Object num)(Code) | | Converts an object with a numeric value into a Number
Valid formats are
Number or a
String representation of a number. Note that this does not
handle localized number formats. Use the
NumberTool to handle such conversions.
Parameters: num - the number to be converted a Number representation of the numberor null if it's invalid |
|
|