Math helpers

Math helpers transform number or integer content in Handlebars expressions.

 Important

  • These helpers work only with numbers and properties that resolve to numbers or integers.

  • You cannot nest math operations or use other helpers inside a math helper. For example, you cannot use $def to set a default value for a property in a math helper.

  • Take care to use properties that you know exist and have number/integer values, so that your message makes sense to your audience.

Add

add — Adds number. Add helpers take an unlimited number of arguments. For example, {{add 5 2 2}} resolves to 9.

Properties

  • number — Variable that contains a number value, no limit on values that can be added for a sum total. You can add as many numbers as fits your message.
FormatExampleOutput
{{add number1 number2...}}You can have {{add slices_per_person left_overs}} slices of pizza!You can have 3 slices of pizza!

Subtract

sub — Subtracts number. Subtract helpers take an unlimited number of arguments. For example, {{sub 5 2 2}} resolves to 1. You can subtract as many numbers as fits your message.

Properties

  • number — Variable that contains a number value or an integer.
FormatExampleOutput
{{sub number1 number2...}}You can have {{sub slices_per_person left_overs}} more slice of pizza!You can have 1 more slice of pizza!

Multiply

mul — Multiplies number. Multiplication helpers take an unlimited number of arguments. For example, {{mul 4 2 2}} resolves to 16.

Properties

  • number — Variable that contains a number value or an integer.
FormatExampleOutput
{{mul number1 number2...}}You need {{mul slices_per_person people}} total pizza slices for the party.You need 36 total pizza slices for the party.

Divide

div — Divides number. Division helpers take an unlimited number of arguments. For example, {{div 4 2 2}} resolves to 1.

Properties

  • number — Variable that contains a number value or an integer.
FormatExampleOutput
{{div number1 number2...}}Pizza partiers can each have {{div total_slices people}} slices.Pizza partiers can each have 3 slices.

Modulo

mod — Returns the remainder when dividing two number. This handler takes exactly two arguments.

Properties

  • number — Variable that contains a number value or an integer.
FormatExampleOutput
{{mod number1 number2}}Pizza slices up for grabs: {{mod total_slices slices_per_person}}Pizza slices up for grabs: 2

Rounding numbers

round — Rounds number to the nearest integer. You can round to the nearest decimal place by adding second argument determining the decimal place you want to round to.

Properties

  • number — Variable that contains a number value or an integer.
  • place — Optional. Numerical value to round to a decimal place.

Round to the nearest integer

FormatExampleOutput
{{round number}}Pi is pretty close to {{round pi}}Pi is pretty close to 3

Round to a decimal place

FormatExampleOutput
{{mod number place}}Pi is pretty close to {{round pi 2}}Pi is pretty close to 3.14

Ceiling

$ceil — Rounds up to the nearest integer.

Properties

  • number — The number to be rounded.
FormatExampleOutput
{{$ceil number}}{{$ceil 3.001}}4

Floor

$floor — Rounds down to the nearest integer.

Properties

  • number — The number to be rounded.
FormatExampleOutput
{{$floor number}}{{$floor 3.999}}3