login | register
Thu 04 of Dec, 2008 [01:50 UTC]

voip-info.org

Discuss [2] History

Asterisk func math

Created by: murf,Last modification on Wed 23 of May, 2007 [12:18 UTC] by hermuli

Synopsis:

 Performs Mathematical Functions

Description:

 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


See also



Comments

Comments Filter
222

333example using Math()

by baji, Friday 13 of July, 2007 [14:21:55 UTC]
;
; ----------- ( 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()
;
; ------------------------------------
222

333Problems with using ',' as an argument separator

by jbitch, Thursday 28 of September, 2006 [14:49:46 UTC]
Sometime ago it was decided to go in the direction of using '|' (pipe character) as the argument separator for
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.