# MathTool documentation for the dotCMS Content Management System

The MathTool viewtool provides methods to perform floating point math in Velocity. It utilizes the `java.lang.Math` class to perform calculations.

The following example shows how the ListTool is mapped in the toolbox-xml file:

``````    <tool>
<key>math</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.MathTool</class>
</tool>
``````

### Number Format

Most methods in this tool return numbers wrapped as Double, which automatically render the decimal places, even for whole numbers. For example, the call `\$math.add(1, 2).toString()` returns “3.0” rather than just “3”.

• This is intentional.
• The MathTool tool is intended for floating point arithmetic.
• Integer arithmetic is already supported directly by the Velocity template language.
• If you need to convert the result of a MathTool method to an integer, use the `\$math.toInteger()` method or call the built-in Velocity `intValue()` method on the result; for example:
``````\$math.toInteger(\$math.add(1, 2))
``````

### Exception Handling

None of the MathTool methods throw exceptions for null pointers, number formats, or divide by zero.

• This is intentional (the methods are designed to intentionally fail silently), because throwing these exceptions would halt HTML rendering.
• However even though no exceptions are thrown, you can identify errors, because when an error occurs Velocity will render the reference literally (e.g. `\$math.div(1, 0)` renders as “\$math.div(1, 0)“).

## Methods and Usage

The MathTool supports the following methods:

MethodOperationReturn Value
`\$math.add( x, y )`Addition`x` plus `y`.
`\$math.sub( x, y )`Subtraction`x` minus `y`.
`\$math.mul( x, y )`Multiplication`x` multiplied by `y`.
`\$math.div( x, y )`Division`x` divided by `y`.
`\$math.mod( x, y )`ModulusThe integer remainder of integer `x` divided by integer `y`.
`\$math.pow( x, y )`Power (Exponent)`x` raised to the power of `y`.
`\$math.abs( x )`Absolute valueThe absolute value of `x`.
`\$math.max( x, y )`MaximumThe maximum of `x` and `y`.
Note: This method accepts a maximum of 2 arguments.
`\$math.min( x, y )`MinimumThe minimum of `x` and `y`.
Note: This method accepts a maximum of 2 arguments.
`\$math.ceil( x )`CeilingThe smallest integer greater than or equal to `x`.
`\$math.floor( x )`FloorThe integer portion of `x`.
`\$math.round( x )`Rounding`x` rounded to the nearest whole integer.
`\$math.roundTo( x, y )`Rounding`x` rounded to `y` decimal places.
`\$math.getAverage( l )`AverageThe average of all values in list or collection 'l'.
See Parameters, below
`\$math.getTotal( l )`Total (Sum)The sum of all values in list or collection 'l'.
See Parameters, below
`\$math.getRandom()`Random numberA pseudo-random number in the range from 0 to 1.
`\$math.random( x, y )`Random number in rangeA pseudo-random number in the range from `x` to `y`.
`\$math.toDouble( x )`Double conversion`x` converted to a double.
See Parameters, below.
`\$math.toInteger( x )`Integer conversion`x` converted to an integer.
See Parameters, below.

### Parameters

With the exception of the methods listed below, all arguments supplied to all methods must be numbers. Integer arguments will be automatically converted to floating point numbers when supplied as arguments.

Methods accepting arguments other than numbers:

MethodParameter Type(s)Example
getAverage()A list or collection containing only numbers.`\$math.getAverage( [ 1, 2, 3 ] )`
getTotal()A list or collection containing only numbers.`\$math.getTotal( [ 1, 2, 3 ] )`
getRandom()Does not take any arguments.`getRandom()`
toDouble()Either a number or a string containing a numeric format.`\$math.toDouble( 5 )`
`\$math.toDouble( "5.6" )`
toInteger()Either a number or a string containing a numeric format.`\$math.toInteger( 5.6 )`
`\$math.toInteger( "5" )`

## Math in Velocity

If you are performing only simple mathematical operations, you may be able to perform them without using the MathTool. Velocity supports simple math through the use of the `#set()` directive, including addition, subtraction, multiplication, division and modulus. The following table shows how to use `#set` directive mathematical operations, in comparison to using the MathTool:

OperatorMathToolVelocity
Addition`#set(\$result = \$math.add(\$value1, 1))``#set(\$result = \$value1 + 1)`
Subtraction`#set(\$result = \$math.sub(\$value1, 2))``#set(\$result = \$value1 - 2)`
Multiplication`#set(\$result = \$math.mul(\$value1, 3))``#set(\$result = \$value1 * 3)`
Division`#set(\$result = \$math.div(\$value1, 4))``#set(\$result = \$value1 / 4)`
Modulus`#set(\$result = \$math.mod(\$value1, 5))``#set(\$result = \$value1 % 5)`

Note: The Velocity `#set` directive can perform mathematical operations on both integers and floating point numbers. However operations on floating point numbers are not precise, and may contain errors in far decomal places, so if calculation accuracy is important, you should use the MathTool instead of Velocity.
{"ft_lang_label":"__label__en","ft_lang_prob":0.5709436,"math_prob":0.96875054,"size":4969,"snap":"2020-34-2020-40","text_gpt3_token_len":1268,"char_repetition_ratio":0.1327291,"word_repetition_ratio":0.08499336,"special_character_ratio":0.2559871,"punctuation_ratio":0.16344085,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9992416,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-29T03:34:20Z\",\"WARC-Record-ID\":\"<urn:uuid:4df47a67-ff12-4355-8f29-df062ade20a6>\",\"Content-Length\":\"93301\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8bd73f99-ac19-42e6-aaba-70006671df94>\",\"WARC-Concurrent-To\":\"<urn:uuid:6298634f-5a61-4ca1-a2e1-4892daa9ca6a>\",\"WARC-IP-Address\":\"3.22.136.122\",\"WARC-Target-URI\":\"https://dotcms.com/docs/latest/mathtool\",\"WARC-Payload-Digest\":\"sha1:VZLNNFNX7M5TJI5CCCG3CUQBDXXINUAY\",\"WARC-Block-Digest\":\"sha1:HNJD7GHVA64CQZSSBRT4YYFDP62PTK26\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600401624636.80_warc_CC-MAIN-20200929025239-20200929055239-00064.warc.gz\"}"}