org.apache.velocity.tools.generic
Class MathTool

java.lang.Object
  |
  +--org.apache.velocity.tools.generic.MathTool

public class MathTool
extends java.lang.Object

Tool for performing floating point math in Velocity.

Several things should be noted here:

 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>
 

Version:
$Revision: 1.4 $ $Date: 2003/11/06 06:48:32 $
Author:
Nathan Bubna

Constructor Summary
MathTool()
           
 
Method Summary
 java.lang.Double abs(java.lang.Object num)
           
 java.lang.Double add(java.lang.Object num1, java.lang.Object num2)
           
 java.lang.Double div(java.lang.Object num1, java.lang.Object num2)
           
 java.lang.Double getRandom()
           
 java.lang.Double max(java.lang.Object num1, java.lang.Object num2)
           
 java.lang.Double min(java.lang.Object num1, java.lang.Object num2)
           
 java.lang.Double mul(java.lang.Object num1, java.lang.Object num2)
           
 java.lang.Double pow(java.lang.Object num1, java.lang.Object num2)
           
 java.lang.Integer random(java.lang.Object num1, java.lang.Object num2)
          This returns a random Integer within the specified range.
 java.lang.Double roundTo(java.lang.Object decimals, java.lang.Object num)
          Rounds a number to the specified number of decimal places.
 java.lang.Integer roundToInt(java.lang.Object num)
          Rounds a number to the nearest whole Integer
 java.lang.Double sub(java.lang.Object num1, java.lang.Object num2)
           
 java.lang.Double toDouble(java.lang.Object num)
          Converts an object with a numeric value into a Double Valid formats are Number or a String representation of a number
 java.lang.Integer toInteger(java.lang.Object num)
          Converts an object with a numeric value into an Integer Valid formats are Number or a String representation of a number
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MathTool

public MathTool()
Method Detail

add

public java.lang.Double add(java.lang.Object num1,
                            java.lang.Object num2)
Parameters:
num1 - the first number
num2 - the second number
Returns:
the sum of the numbers or null if they're invalid
See Also:
toDouble(java.lang.Object)

sub

public java.lang.Double sub(java.lang.Object num1,
                            java.lang.Object num2)
Parameters:
num1 - the first number
num2 - the second number
Returns:
the difference of the numbers or null if they're invalid
See Also:
toDouble(java.lang.Object)

mul

public java.lang.Double mul(java.lang.Object num1,
                            java.lang.Object num2)
Parameters:
num1 - the first number
num2 - the second number
Returns:
the product of the numbers or null if they're invalid
See Also:
toDouble(java.lang.Object)

div

public java.lang.Double div(java.lang.Object num1,
                            java.lang.Object num2)
Parameters:
num1 - the first number
num2 - the second number
Returns:
the quotient of the numbers or null if they're invalid
See Also:
toDouble(java.lang.Object)

pow

public java.lang.Double pow(java.lang.Object num1,
                            java.lang.Object num2)
Parameters:
num1 - the first number
num2 - the second number
Returns:
the first number raised to the power of the second or null if they're invalid
See Also:
toDouble(java.lang.Object)

max

public java.lang.Double max(java.lang.Object num1,
                            java.lang.Object num2)
Parameters:
num1 - the first number
num2 - the second number
Returns:
the largest of the numbers or null if they're invalid
See Also:
toDouble(java.lang.Object)

min

public java.lang.Double min(java.lang.Object num1,
                            java.lang.Object num2)
Parameters:
num1 - the first number
num2 - the second number
Returns:
the smallest of the numbers or null if they're invalid
See Also:
toDouble(java.lang.Object)

abs

public java.lang.Double abs(java.lang.Object num)
Returns:
the absolute value of the number null if it's invalid
See Also:
toDouble(java.lang.Object)

toDouble

public java.lang.Double toDouble(java.lang.Object num)
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
Returns:
a Double representation of the number or null if it's invalid

toInteger

public java.lang.Integer toInteger(java.lang.Object num)
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
Returns:
a Integer representation of the number or null if it's invalid

roundTo

public java.lang.Double roundTo(java.lang.Object decimals,
                                java.lang.Object num)
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 roundToInt(java.lang.Object), as that will return an Integer rather than a Double.

Parameters:
decimals - the number of decimal places
Returns:
the value rounded to the specified number of decimal places or null if it's invalid
See Also:
toDouble(java.lang.Object), toInteger(java.lang.Object)

roundToInt

public java.lang.Integer roundToInt(java.lang.Object num)
Rounds a number to the nearest whole Integer

Parameters:
num - the number to round
Returns:
the number rounded to the nearest whole Integer or null if it's invalid
See Also:
toDouble(java.lang.Object)

getRandom

public java.lang.Double getRandom()
Returns:
a pseudo-random Double greater than or equal to 0.0 and less than 1.0
See Also:
Math.random()

random

public java.lang.Integer random(java.lang.Object num1,
                                java.lang.Object num2)
This returns a random Integer within the specified range. The return Integer will have a value greater than or equal to the first number and less than the second number.

Parameters:
num1 - the first number
num2 - the second number
Returns:
a pseudo-random Integer greater than or equal to the first number and less than the second
See Also:
toInteger(java.lang.Object), Math.random()


Copyright (c) 2003 Apache Software Foundation