@DefaultKey(value="math") @ValidScope(value="application") public class MathTool extends FormatConfig implements Serializable
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>
DEFAULT_FORMAT, FORMAT_KEY
DEFAULT_LOCALE
LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY
Constructor and Description |
---|
MathTool() |
Modifier and Type | Method and Description |
---|---|
Number |
abs(Object num) |
Number |
add(Object... nums) |
Number |
add(Object num1,
Object num2) |
Number |
bitwiseAnd(Object... nums)
Bitwise And
|
Number |
bitwiseAnd(Object num1,
Object num2) |
Number |
bitwiseOr(Object... nums)
Bitwise Or
|
Number |
bitwiseOr(Object num1,
Object num2) |
Number |
bitwiseXOr(Object... nums)
Bitwise XOr
|
Number |
bitwiseXOr(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) |
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)
Deprecated.
use
NumberTool.toNumber(Object) .doubleValue() |
Integer |
toInteger(Object num)
Deprecated.
use
NumberTool.toNumber(Object) .intValue() |
Long |
toLong(Object num)
Deprecated.
use
NumberTool.toNumber(Object) .longValue() |
Number |
toNumber(Object num)
Deprecated.
|
configure, getFormat, setFormat
getLocale, setLocale, toLocale
configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
public Number add(Object... nums)
nums
- the numbers to be addednull
if they're invalidtoNumber(java.lang.Object)
public Number sub(Object... nums)
nums
- the numbers to be subtractednull
if they're invalidtoNumber(java.lang.Object)
public Number mul(Object... nums)
nums
- the numbers to be multipliednull
if they're invalidtoNumber(java.lang.Object)
public Number div(Object... nums)
nums
- the numbers to be dividednull
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 numbernull
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 numbertoInteger(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 numbertoInteger(java.lang.Object)
public Number bitwiseOr(Object... nums)
nums
- the numbers to be Or'ednull
if they're invalidtoInteger(java.lang.Object)
public Number bitwiseAnd(Object... nums)
nums
- the numbers to be And'ednull
if they're invalidtoInteger(java.lang.Object)
public Number bitwiseXOr(Object... nums)
nums
- the numbers to be XOr'ednull
if they're invalidtoInteger(java.lang.Object)
public Number max(Object... nums)
nums
- the numbers to be searchednull
if they're invalidtoNumber(java.lang.Object)
public Number min(Object... nums)
nums
- the numbers to be searchednull
if they're invalidtoNumber(java.lang.Object)
public Number abs(Object num)
num
- the numbernull
if it's invalidtoDouble(java.lang.Object)
public Integer ceil(Object num)
num
- the numberpublic Integer floor(Object num)
num
- the numberpublic Integer round(Object num)
num
- the number to roundnull
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 roundnull
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 numberNumber
greater than
or equal to the first number and less than
the secondMath.random()
@Deprecated public Integer toInteger(Object num)
NumberTool.toNumber(Object)
.intValue()Number
or a String
representation of a numbernum
- the number to be convertedInteger
representation of the number
or null
if it's invalid@Deprecated public Long toLong(Object num)
NumberTool.toNumber(Object)
.longValue()Number
or a String
representation of a numbernum
- the number to be convertedLong
integer representation of the number
or null
if it's invalid@Deprecated public Double toDouble(Object num)
NumberTool.toNumber(Object)
.doubleValue()Number
or a String
representation of a numbernum
- the number to be convertedDouble
representation of the number
or null
if it's invalid@Deprecated public Number toNumber(Object num)
NumberTool.toNumber(Object)
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 convertedNumber
representation of the number
or null
if it's invalidprotected Number matchType(Number in, double out)
in
- instance of wanted Number classout
- input numbermatchType(double,Number...)
protected Number matchType(Number in1, Number in2, double out)
in1
- instance #1 of wanted Number classin2
- instance #1 of wanted Number classout
- input numbermatchType(double,Number...)
protected Number matchType(double out, Number... in)
out
- target number valuein
- wanted Number classesprotected boolean hasFloatingPoint(String value)
value
- target valuepublic 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 valuespublic Number getAverage(Collection collection)
collection
- A collection containing number valuespublic Number getTotal(Object... array)
array
- An array containing number valuespublic Number getAverage(Object... array)
array
- An array containing number valuespublic Number getTotal(double... values)
values
- The list of double values to add up.public Number getAverage(double... values)
values
- The list of double valuespublic Number getTotal(long... values)
values
- The list of long values to add up.public Number getAverage(long... values)
values
- The list of long valuesCopyright © 2002–2021 The Apache Software Foundation. All rights reserved.