Math Formula Task¶
Overview¶
The Math Formula Task evaluates mathematical expressions using the powerful mathjs library. Calculate totals, apply discounts, convert units, compute scores, or perform any mathematical operation with support for complex formulas, functions, and constants.
When to use this task:
- Price calculations and discounts
- Invoice totals with tax and fees
- Unit conversions (weight, distance, currency)
- Scoring systems with weights
- Commission calculations
- Time and date arithmetic
- Statistical computations
- Financial formulas
Key Features:
- Full mathjs expression support
- Basic arithmetic (+, -, *, /, ^)
- Mathematical functions (sqrt, abs, round, sin, cos, etc.)
- Constants (pi, e)
- Ternary operators for conditionals
- Variable replacement
- Automatic 2-decimal rounding
- Error handling
Quick Start¶
1. Add Math Formula task
2. Enter formula with operators and functions
3. Use variables from previous tasks
4. Result rounded to 2 decimals
5. Save
Configuration¶
Builder fields¶
The Field column is the label as it appears in the task builder; Key is the name the value is stored under and referenced by.
| Field | Key | Type | Default | Notes |
|---|---|---|---|---|
| Formula | Formula |
monospace | – |
Formula¶
Formula: 100 * 0.9
Formula: {{subtotal}} + ({{subtotal}} * 0.15) + {{shipping}}
Formula: sqrt({{value}}) * 2.5
Formula: {{sales}} > 10000 ? {{sales}} * 0.15 : {{sales}} * 0.10
Supports:
- Variables:
{{task_X001_field}} - Operators:
+,-,*,/,^(power) - Parentheses:
(,) - Functions:
sqrt(),abs(),round(),ceil(),floor(),min(),max(), etc. - Constants:
pi,e - Conditionals:
condition ? true_value : false_value
Output Fields¶
| Field | Description | Example |
|---|---|---|
task_27001_run |
Success status | true |
task_27001_run_text |
Result message | Successfully evaluated formula |
task_27001_formula |
Original formula (echoed) | 100 * 0.9 |
task_27001_value |
Calculated result (2 decimals) | 90.00 |
Common Operations¶
Basic Arithmetic¶
# Addition
Formula: 100 + 50
Result: 150.00
# Subtraction
Formula: 1000 - 150
Result: 850.00
# Multiplication
Formula: 25 * 4.99
Result: 124.75
# Division
Formula: 1000 / 12
Result: 83.33
# Power
Formula: 2 ^ 10
Result: 1024.00
Functions¶
# Square root
Formula: sqrt(144)
Result: 12.00
# Absolute value
Formula: abs(-50)
Result: 50.00
# Rounding
Formula: round(3.7)
Result: 4.00
Formula: ceil(3.2)
Result: 4.00
Formula: floor(3.8)
Result: 3.00
# Min/Max
Formula: min(10, 25, 5, 18)
Result: 5.00
Formula: max(10, 25, 5, 18)
Result: 25.00
Constants¶
# Pi
Formula: pi * 2
Result: 6.28
# Euler's number
Formula: e ^ 2
Result: 7.39
# Circle area
Formula: pi * (5 ^ 2)
Result: 78.54
Conditional (Ternary)¶
# If-then-else
Formula: {{value}} > 100 ? {{value}} * 0.1 : {{value}} * 0.05
If value = 150: Result = 15.00 (10%)
If value = 50: Result = 2.50 (5%)
Real-World Examples¶
Work out a discounted total¶
Form Submission
└─ Math Formula {{task_55001_price}} * 0.85
└─ Email confirm the total as {{task_27001_value}}
{{task_27001_formula}} returns the expression that was evaluated, which is what to check when a
result looks wrong.
Convert a call duration to minutes¶
Call Tracking Trigger
└─ Math Formula {{task_51001_talk_duration}} / 60
└─ Workflow Note log {{task_27001_value}} minutes against the client
Score a lead from form answers¶
Form Submission
└─ Math Formula ({{task_55001_budget}} / 1000) + ({{task_55001_urgency}} * 5)
└─ If Statement {{task_27001_value}} greater than 50 → route to senior sales
Best Practices¶
Formula Design¶
- Use parentheses - Ensure correct order of operations
- Test with known values - Verify formula correctness
- Handle edge cases - Consider division by zero, negative values
- Document complex formulas - Add comments in workflow description
- Break down complex calculations - Use multiple formula tasks
Variable Usage¶
# Clear variable names
{{task_43001_subtotal}} + {{task_43001_tax}}
# Not this
{{task_1_val}} + {{task_2_val}}
Error Prevention¶
# Avoid division by zero
Formula: {{numerator}} / ({{denominator}} == 0 ? 1 : {{denominator}})
# Handle negative values
Formula: abs({{value}})
# Ensure positive results
Formula: max(0, {{calculated_value}})
Precision¶
- Results always rounded to 2 decimals
- For more precision, use Code task
- For currency, 2 decimals is standard
Troubleshooting¶
Invalid Formula Error¶
Issue: Task returns error, value = 0
Causes:
- Syntax error in formula
- Undefined variable
- Division by zero
- Invalid function name
Solution:
# Test formula with actual values first
Formula: 100 + 50 # Test basic formula
# Then add variables
Formula: {{value1}} + {{value2}}
# Check variable exists
Use If task to verify before Math task
Unexpected Result¶
Issue: Wrong value calculated
Causes:
- Order of operations
- Variable not replaced
- Incorrect formula logic
Solution:
# Use parentheses
Wrong: 100 + 50 * 2 = 200
Right: (100 + 50) * 2 = 300
# Debug with Code task
Code: return {debug: input.task_X001_field};
Variable Not Replaced¶
Issue: Formula contains {{...}} in output
Cause: Variable doesn't exist or is empty
Solution:
- Verify previous task ran successfully
- Check variable name spelling
- Use Coalesce for fallback values
Frequently Asked Questions¶
Can I use complex mathjs functions?¶
Yes, full mathjs library supported. See mathjs.org for complete function list.
What's the precision of calculations?¶
Internal precision is JavaScript number (64-bit float). Output rounded to 2 decimals.
Can I avoid rounding?¶
No, 2-decimal rounding is fixed. Use Code task for custom precision.
How do I handle currency?¶
2-decimal rounding is perfect for currency. Always results like 19.99, never 19.987.
Can formulas span multiple lines?¶
No, single-line only. Break complex calculations into multiple Math tasks.
Are trigonometric functions supported?¶
Yes: sin(), cos(), tan(), asin(), acos(), atan(), etc.
Can I use variables in ternary conditions?¶
Yes: {{value}} > {{threshold}} ? {{value}} * 0.1 : 0
Related Tasks¶
- Variable - Store calculated values for reuse
- Code Task - Complex calculations and custom precision
- If Task - Conditional logic based on calculated values
- Formatter - Format numbers with thousands separators
- Coalesce - Provide fallback values for missing data