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:

  1. This class does not have methods that take primitives. This is simply because Velocity wraps all primitives for us automagically.
  2. Most methods return Double wrappers which automatically render the decimal places even for whole numbers (e.g. new Double(1).toString() -> '1.0') This is intentional. This tool is for floating point arithmetic. Integer arithmetic is already supported in Velocity syntax. if you really need '1' instead of '1.0', just call intValue() on the result.
  3. 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)')
  4. Version:
    $Revision: 1.2 $ $Date: 2003/05/28 00:17:15 $
    Author:
    Nathan Bubna

    Constructor Summary
    MathTool()
               
     
    Method Summary
    static java.lang.Double abs(java.lang.Object num)
               
    static java.lang.Double add(java.lang.Object num1, java.lang.Object num2)
               
    static java.lang.Double div(java.lang.Object num1, java.lang.Object num2)
               
    static java.lang.Double getRandom()
               
    static java.lang.Double max(java.lang.Object num1, java.lang.Object num2)
               
    static java.lang.Double min(java.lang.Object num1, java.lang.Object num2)
               
    static java.lang.Double mul(java.lang.Object num1, java.lang.Object num2)
               
    static java.lang.Double pow(java.lang.Object num1, java.lang.Object num2)
               
    static java.lang.Integer random(java.lang.Object num1, java.lang.Object num2)
              This returns a random Integer within the specified range.
    static java.lang.Double roundTo(java.lang.Object decimals, java.lang.Object num)
              Rounds a number to the specified number of decimal places.
    static java.lang.Integer roundToInt(java.lang.Object num)
              Rounds a number to the nearest whole Integer
    static java.lang.Double sub(java.lang.Object num1, java.lang.Object num2)
               
    static 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
    static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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