|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.velocity.tools.generic.SafeConfig
org.apache.velocity.tools.generic.LocaleConfig
org.apache.velocity.tools.generic.FormatConfig
org.apache.velocity.tools.generic.MathTool
@DefaultKey(value="math") public class MathTool
Tool for performing math in Velocity.
Some things should be noted here:
Example tools.xml config: <tools> <toolbox scope="application"> <tool class="org.apache.velocity.tools.generic.MathTool"/> </toolbox> </tools>
Field Summary |
---|
Fields inherited from class org.apache.velocity.tools.generic.FormatConfig |
---|
DEFAULT_FORMAT, FORMAT_KEY |
Fields inherited from class org.apache.velocity.tools.generic.LocaleConfig |
---|
DEFAULT_LOCALE |
Fields inherited from class org.apache.velocity.tools.generic.SafeConfig |
---|
LOCK_CONFIG_KEY, OLD_LOCK_CONFIG_KEY, SAFE_MODE_KEY |
Constructor Summary | |
---|---|
MathTool()
|
Method Summary | |
---|---|
Number |
abs(Object num)
|
Number |
add(Object... nums)
|
Number |
add(Object num1,
Object num2)
|
Integer |
ceil(Object num)
|
Number |
div(Object... nums)
|
Number |
div(Object num1,
Object num2)
|
Integer |
floor(Object num)
|
Number |
getAverage(Collection collection)
Get the average of the values |
Number |
getAverage(Collection collection,
String field)
Get the average of the values from a list |
Number |
getAverage(double... values)
Get the average of the values in an array of double values |
Number |
getAverage(long... values)
Get the average of the values in an array of long values |
Number |
getAverage(Object... array)
Get the average of the values |
Number |
getAverage(Object[] array,
String field)
Get the sum of the values from a list |
Double |
getRandom()
|
Number |
getTotal(Collection collection)
Get the sum of the values |
Number |
getTotal(Collection collection,
String field)
Get the sum of the values from a list |
Number |
getTotal(double... values)
Get the sum of the values |
Number |
getTotal(long... values)
Get the sum of the values |
Number |
getTotal(Object... array)
Get the sum of the values |
Number |
getTotal(Object[] array,
String field)
Get the sum of the values from a list |
protected boolean |
hasFloatingPoint(String value)
|
Integer |
idiv(Object num1,
Object num2)
Does integer division on the int values of the specified numbers. |
protected Number |
matchType(double out,
Number... in)
Takes the original argument(s) and returns the resulting value as an instance of the best matching type (Integer, Long, or Double). |
protected Number |
matchType(Number in,
double out)
|
protected Number |
matchType(Number in1,
Number in2,
double out)
|
Number |
max(Object... nums)
|
Number |
max(Object num1,
Object num2)
|
Number |
min(Object... nums)
|
Number |
min(Object num1,
Object num2)
|
Integer |
mod(Object num1,
Object num2)
Does integer modulus on the int values of the specified numbers. |
Number |
mul(Object... nums)
|
Number |
mul(Object num1,
Object num2)
|
protected Number |
parseNumber(String value)
Deprecated. |
Number |
pow(Object num1,
Object num2)
|
Number |
random(Object num1,
Object num2)
This returns a random Number within the
specified range. |
Integer |
round(Object num)
Rounds a number to the nearest whole Integer |
Double |
roundTo(Object decimals,
Object num)
Rounds a number to the specified number of decimal places. |
Number |
sub(Object... nums)
|
Number |
sub(Object num1,
Object num2)
|
Double |
toDouble(Object num)
Converts an object with a numeric value into a Double Valid formats are Number or a String
representation of a number |
Integer |
toInteger(Object num)
Converts an object with a numeric value into an Integer Valid formats are Number or a String
representation of a number |
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. |
Methods inherited from class org.apache.velocity.tools.generic.FormatConfig |
---|
configure, getFormat, setFormat |
Methods inherited from class org.apache.velocity.tools.generic.LocaleConfig |
---|
getLocale, setLocale |
Methods inherited from class org.apache.velocity.tools.generic.SafeConfig |
---|
configure, isConfigLocked, isSafeMode, setLockConfig, setSafeMode |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public MathTool()
Method Detail |
---|
public Number add(Object num1, Object num2)
public Number sub(Object num1, Object num2)
public Number mul(Object num1, Object num2)
public Number div(Object num1, Object num2)
public Number max(Object num1, Object num2)
public Number min(Object num1, Object num2)
public Number add(Object... nums)
nums
- the numbers to be added
null
if they're invalidtoNumber(java.lang.Object)
public Number sub(Object... nums)
nums
- the numbers to be subtracted
null
if they're invalidtoNumber(java.lang.Object)
public Number mul(Object... nums)
nums
- the numbers to be multiplied
null
if they're invalidtoNumber(java.lang.Object)
public Number div(Object... nums)
nums
- the numbers to be divided
null
if they're invalid
or if any denominator equals zerotoNumber(java.lang.Object)
public Number pow(Object num1, Object num2)
num1
- the first numbernum2
- the second number
null
if they're invalidtoNumber(java.lang.Object)
public Integer idiv(Object num1, Object num2)
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
toInteger(java.lang.Object)
public Integer mod(Object num1, Object num2)
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
toInteger(java.lang.Object)
public Number max(Object... nums)
nums
- the numbers to be searched
null
if they're invalidtoNumber(java.lang.Object)
public Number min(Object... nums)
nums
- the numbers to be searched
null
if they're invalidtoNumber(java.lang.Object)
public Number abs(Object num)
num
- the number
null
if it's invalidtoDouble(java.lang.Object)
public Integer ceil(Object num)
num
- the number
public Integer floor(Object num)
num
- the number
public Integer round(Object num)
num
- the number to round
null
if it's invalidMath.rint(double)
public Double roundTo(Object decimals, Object num)
round(java.lang.Object)
, as that will return
an Integer
rather than a Double
.
decimals
- the number of decimal placesnum
- the number to round
null
if it's invalidtoNumber(java.lang.Object)
public Double getRandom()
Double
greater
than or equal to 0.0 and less than 1.0Math.random()
public Number random(Object num1, Object num2)
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.
num1
- the first numbernum2
- the second number
Number
greater than
or equal to the first number and less than
the secondMath.random()
public Integer toInteger(Object num)
Number
or a String
representation of a number
num
- the number to be converted
Integer
representation of the number
or null
if it's invalidpublic Double toDouble(Object num)
Number
or a String
representation of a number
num
- the number to be converted
Double
representation of the number
or null
if it's invalidpublic Number toNumber(Object num)
Number
or a String
representation of a number. Note that this does not
handle localized number formats. Use the NumberTool
to handle such conversions.
num
- the number to be converted
Number
representation of the number
or null
if it's invalidprotected Number matchType(Number in, double out)
matchType(double,Number...)
protected Number matchType(Number in1, Number in2, double out)
matchType(double,Number...)
protected Number matchType(double out, Number... in)
protected boolean hasFloatingPoint(String value)
@Deprecated protected Number parseNumber(String value)
public Number getTotal(Collection collection, String field)
collection
- A collection containing Java beansfield
- A Java Bean field for the objects in collection that
will return a number.
public Number getAverage(Collection collection, String field)
collection
- A collection containing Java beansfield
- A Java Bean field for the objects in collection that
will return a number.
public Number getTotal(Object[] array, String field)
array
- An array containing Java beansfield
- A Java Bean field for the objects in array that
will return a number.
public Number getAverage(Object[] array, String field)
array
- A collection containing Java beansfield
- A Java Bean field for the objects in array that
will return a number.
public Number getTotal(Collection collection)
collection
- A collection containing numeric values
public Number getAverage(Collection collection)
collection
- A collection containing number values
public Number getTotal(Object... array)
array
- An array containing number values
public Number getAverage(Object... array)
array
- An array containing number values
public Number getTotal(double... values)
values
- The list of double values to add up.
public Number getAverage(double... values)
values
- The list of double values
public Number getTotal(long... values)
values
- The list of long values to add up.
public Number getAverage(long... values)
values
- The list of long values
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |