Math Functions and Properties

Overview

The Math object provides mathematical functions and constants for performing numeric calculations in expressions. All Math functions work similarly to their JavaScript equivalents, making them familiar to developers with JavaScript experience.

Note: Not all supported methods may be in the Functions and Properties drop-down list. If not available, you may type it in.

Mathematical Functions

Math.abs()

Returns the absolute value of a number.

Syntax: Math.abs(number)

Example:

Math.abs(-1)       // Returns: 1
Math.abs(5.2)      // Returns: 5.2
Math.abs(-15.7)    // Returns: 15.7

Math.ceil()

Returns the smallest integer greater than or equal to a number (rounds up).

Syntax: Math.ceil(number)

Example:

Math.ceil(3342.3)   // Returns: 3343
Math.ceil(5.1)      // Returns: 6
Math.ceil(-5.9)     // Returns: -5

Math.floor()

Returns the largest integer less than or equal to a number (rounds down).

Syntax: Math.floor(number)

Example:

Math.floor(3342.3)  // Returns: 3342
Math.floor(5.9)     // Returns: 5
Math.floor(-5.1)    // Returns: -6

Math.round()

Returns the value rounded to the nearest integer.

Syntax: Math.round(number)

Example:

Math.round(3.14159265358979)   // Returns: 3
Math.round(3.64159265358979)   // Returns: 4
Math.round(-3.14159265358979)  // Returns: -3

Math.trunc()

Returns the integral number, removing any fractional digits (truncates toward zero).

Syntax: Math.trunc(number)

Example:

Math.trunc(14.29)   // Returns: 14
Math.trunc(-14.29)  // Returns: -14
Math.trunc(0.123)   // Returns: 0

Math.sign()

Returns the sign of a number, indicating if it is positive, negative, zero, or not a number.

Syntax: Math.sign(number)

Returns:

  • 1 for positive numbers
  • -1 for negative numbers
  • 0 for zero (both positive and negative zero)
  • NaN for non-numeric values

Example:

Math.sign('x')    // Returns: NaN
Math.sign('-5')   // Returns: -1
Math.sign(42)     // Returns: 1
Math.sign(0)      // Returns: 0
Note: Unlike JavaScript Math.sign(), SnapLogic treats negative and positive zero as positive zero.

Min/Max Functions

Math.max()

Returns the largest number in a series of supplied numbers.

Syntax: Math.max(value1, value2, ...)

Example:

Math.max(10, 12, 30)          // Returns: 30
Math.max(5.5, 5.6, 5.4)       // Returns: 5.6
Math.max(...$numbers)         // Using spread operator with array

Math.min()

Returns the smallest number in a series of supplied numbers.

Syntax: Math.min(value1, value2, ...)

Example:

Math.min(10, 12, 30)          // Returns: 10
Math.min(5.5, 5.6, 5.4)       // Returns: 5.4
Math.min(...$numbers)         // Using spread operator with array

Power and Root Functions

Math.pow()

Returns the base raised to the exponent power.

Syntax: Math.pow(base, exponent)

Example:

Math.pow(9, 1.5)    // Returns: 27
Math.pow(2, 3)      // Returns: 8
Math.pow(5, 2)      // Returns: 25
Math.pow(10, -2)    // Returns: 0.01

Random Number Functions

Math.random()

Returns a floating-point, pseudo-random number from 0 (inclusive) up to but not including 1.

Syntax: Math.random()

Example:

Math.random()                           // Returns: 0.29628396818343006

// Random integer between 0 and 9
Math.floor(Math.random() * 10)

// Random integer between min and max (inclusive)
Math.floor(Math.random() * (max - min + 1)) + min

Math.randomUUID()

Generates a random UUID (Universally Unique Identifier) string.

Syntax: Math.randomUUID()

Example:

Math.randomUUID()  // Returns: 2c28e2c0-9767-11e4-9a0d-c3f2b01def6d

Useful for generating unique identifiers for records, transactions, or tracking purposes.

Math Constants

The following JavaScript mathematical constants are supported:

Constant Description Approximate Value
Math.E Euler's constant (base of natural logarithm) 2.718281828459045
Math.PI The value of Pi (π) 3.141592653589793
Math.LN2 Natural logarithm of 2 0.6931471805599453
Math.LN10 Natural logarithm of 10 2.302585092994046
Math.LOG2E Base 2 logarithm of e 1.4426950408889634
Math.LOG10E Base 10 logarithm of e 0.4342944819032518
Math.SQRT1_2 Square root of 1/2 0.7071067811865476
Math.SQRT2 Square root of 2 1.4142135623730951

Example Usage:

// Calculate circle circumference
2 * Math.PI * $radius

// Calculate circle area
Math.PI * Math.pow($radius, 2)

// Use Euler's constant
Math.pow(Math.E, 2)

Common Use Cases

Rounding Currency Values:

// Round to 2 decimal places
Math.round($price * 100) / 100

Calculating Percentages:

Math.round(($actual / $total) * 100)  // Returns percentage

Finding Range:

{
  "min": Math.min(...$values),
  "max": Math.max(...$values),
  "range": Math.max(...$values) - Math.min(...$values)
}

Random Selection:

// Select random element from array
$array[Math.floor(Math.random() * $array.length)]

Generate Random ID:

// Unique transaction ID
"TXN-" + Math.randomUUID()

Distance Calculations:

// Euclidean distance
Math.sqrt(Math.pow($x2 - $x1, 2) + Math.pow($y2 - $y1, 2))

Best Practices

  • Precision: Be aware that floating-point arithmetic may have precision limitations. For currency calculations, consider rounding explicitly.
  • Integer Operations: Use Math.floor(), Math.ceil(), or Math.round() when you need integer results from calculations.
  • Constants: Use Math.PI and Math.E constants instead of hardcoding their values for better precision and readability.
  • Random Numbers: Math.random() is suitable for general purposes but not for cryptographic security. For unique IDs, use Math.randomUUID().
  • Type Conversion: Ensure inputs to Math functions are numbers. Use parseInt() or parseFloat() if needed.
  • Array Operations: Use the spread operator (...) with Math.max() and Math.min() to find extremes in arrays.