Synopsis:
Performs Mathematical FunctionsDescription:
MATH(<number1><op><number 2>[,<type_of_result>])Perform calculation on number 1 to number 2. Valid ops are:
+,-,/,*,%,<,>,>=,<=,==
and behave as their C equivalents.
<type_of_result> - wanted type of result:
f, float - float(default)
i, int - integer,
h, hex - hex,
c, char - char
Notes
- *CLI> show function MATH
Return value
Returns the resulting string.Example
exten => s,1,Set(i=${MATH(123%16,int)})
- sets var i=11

Comments
333example using Math()
; ----------- ( using asterisk 1.4.7 ) ---------------
;
; v_Today will approx number of days since THE epoch (not unix epoch)
; v_today = days + ( months * 30 ) + ( years * 365 )
;
exten => s,1,Set(v_years=${CDR(start):0:4}) ; 2007-07-13 09:18:11
exten => s,n,Set(v_months=${CDR(start):5:2})
exten => s,n,Set(v_days=${CDR(start):8:2})
;
; NO spaces on either side of return type (int in this case), or you'll be scratching your head
;
; repeat - no SPACE between int and | or )
;
exten => s,n,Set(v_Today=${MATH( ${v_days} + ${MATH( ${v_months} * 30 |int)} |int)})
exten => s,n,Set(v_Today=${MATH( ${v_Today} + ${MATH( ${v_years} * 365 |int)} |int)})
;
exten => s,n,NoOp(${v_years})
exten => s,n,NoOp(${v_months})
exten => s,n,NoOp(${v_days})
exten => s,n,NoOp(${v_Today})
exten => s,n,Hangup()
;
; ------------------------------------
333Problems with using ',' as an argument separator
applications/functions in asterisk.
I've noticed while playing with the MATH function in asterisk 1.4.0Beta2 that the function does NOT work
correctly if you use a comma as the argument separator.
The technical reason for this is that the macro used to parse the arguments in app.h is actually
hardcoded to a pipe character.
The result for the MATH function is that the return_type argument is ignored, the math you're attempting
is correctly computed and always returned as a float.
Hope that helps anyone scratching their head.