AMPscript Math Functions

The math functions below allow us to add, subtract, divide, and multiply numbers in Marketing Cloud Emails, Landing Pages, SMS/MMS Messages, and Push Notifications. You can write complex math equations to calculate numbers on the fly using these functions.

  • Add(N1,N2)
  • Subtract(N1, N2)
  • Multiply(N1, N2)
  • Divide(N1, N2)
  • Mod(N1, N2)

Add Function

The Add() function in AMPscript is used to add two numeric constants or two variables with a numeric value set.

Example
%%=Add(N1,N2)=%%

<!-- Example Code -->
%%[
/**
* Add Logic
* The Add() is used to add two numeric constants.
*/
VAR @number1, @number2, @results
SET @number1 = 1
SET @number2 = 2
SET @results = Add(@number1,@number2)
]%%
<p>%%=v(@results)=%%</p>
<!-- Example Output -->
3
View this gist on GitHub

Subtract Function

The Subtract() function in AMPscript is used to subtract two numeric constants or two variables with a numeric value set.

Example
%%=Subtract(N1,N2)=%%

<!-- Example Code -->
%%[
/**
* Subtract Logic
* The Subtract() is used to subtract two numeric constants.
*/
VAR @number1, @number2, @results
SET @number1 = 2
SET @number2 = 1
SET @results = Subtract(@number1,@number2)
]%%
<p>%%=v(@results)=%%</p>
<!-- Example Output -->
1
View this gist on GitHub

Multiply Function

The Multiply() function in AMPscript is used to multiply two numeric constants or two variables with a numeric value set.

Example
%%=Multiply(N1,N2)=%%

<!-- Example Code -->
%%[
/**
* Multiply Logic
* The Multiply() is used to multiply two numeric constants.
*/
VAR @number1, @number2, @results
SET @number1 = 2
SET @number2 = 2
SET @results = Multiply(@number1,@number2)
]%%
<p>%%=v(@results)=%%</p>
<!-- Example Output -->
4
View this gist on GitHub

Divide Function

The Divide() function in AMPscript is used to divide two numeric constants or two variables with a numeric value set.

Example
%%=Divide(N1,N2)=%%

<!-- Example Code -->
%%[
/**
* Divide Logic
* The Divide() is used to divide two numeric constants.
*/
VAR @number1, @number2, @results
SET @number1 = 10
SET @number2 = 5
SET @results = Divide(@number1,@number2)
]%%
<p>%%=v(@results)=%%</p>
<!-- Example Output -->
2
View this gist on GitHub

Mod Function

The Mod() function in AMPscript is used to find the remainder between two numeric constants or two variables with a numeric value set.

Example
%%=Mod(N1,N2)=%%

<!-- Example Code -->
%%[
/**
* Mod Logic
* The Mod() is used to find the remainder between two numeric constants.
*/
VAR @number1, @number2, @results
SET @number1 = 10
SET @number2 = 4
SET @results = Mod(@number1,@number2)
]%%
<p>%%=v(@results)=%%</p>
<!-- Example Output -->
2
View this gist on GitHub

Gallery