Documentation for the worlds leading Java Based Open Source Web Content Management System

dotCMS

Numbers and Math

The Math Tool

The Math Tool can performing floating point math in Velocity.

A few things to note:

  • Most methods return numbers wrapped as Double 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 by the Velocity template language. If you really need '1' instead of '1.0', just call intValue() on the result.
  • No null pointer, number format, or divide by zero exceptions are thrown here. This is because such exceptions halt template 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)')
  • Class java.lang.Math is used to perform the mathematical operations.
Class
  org.apache.velocity.tools.generic.MathTool
Name
  org.apache.velocity.tools.generic.MathTool@46588938 (this is the recommended name of the tool in the Velocity context)
Toolbox Configuration Example
 
<tool>
  <key>math</key>
  <scope>application</scope>
  <class>org.apache.velocity.tools.generic.MathTool</class>
</tool>
Method Overview
add() Addition
sub() Subtraction
mul() Multiplication
div() Division
pow() Power of
max() Maximum of two numbers
min() Minimum of two numbers
abs() Absolute value of a number
toDouble() Converts a number into a double.
toInteger() Converts a number into an integer
roundTo() Rounds a number to the specified number of decimal places
roundToInt() Rounds a number to the nearest whole Integer
getRandom() Returns a pseudo-random number
random() Returns a pseudo-random number in a configurable range

For complete documentation on this viewtool use the following link: MathTool

2

Subscribe to dotcms

Visit this group