[math] calculates equations using numbers, dates or time. It can also set variables to be used throughout the page. One common use of a math variable is to create counters.
numero = 76the name of a math variable is limited to 15 alphanumeric characters, and must begin with a letter.
When actually setting a math variable, the value will display, unlike a text variable. To prevent this, add show=f to the [math] context.You can assign multiple math variables at the same time using a semicolon to separate the assignments. Example: [math show=f]var1=1;var2=2[/math].
Practical ExamplesAn invoice template showing the line-item cost of each item[lineitems][quantity], [ref], [price], [math][price]*[quantity][/math][/lineitems]Using a math variable to create a counter
[math show=f]counter=0[/math][founditems][hideif [somecondition]=something][math]counter=counter 1[/math]. [firstname] [lastname]<br>[/hideif][/founditems]There were [counter] names found.In this example, some of the founditems are eliminated via the [hideif]. In this case, [index] and [numfound] would still include the eliminated items, and would not be useful to number the lines, nor to obtain a final count. The first line sets the initial value of [counter] to 0.Date and Time MathDates included in mathematical expressions must be enclosed in curly braces. (You cannot mix both dates and times in one equation.) This converts them to a number (the number of days since 01/01/0000) so math can be performed. You may easily add or subtract days, months, or years from dates by expressing them in date format within curly braces. Just use 0 for values you want ignored. For instance, in order to add 2 months to today's date you would write: [math date]{[date]} {2/0/0000}[/math]. Then to redisplay the result back into date format, add the date modifier to the context, as indicated ([math date]). Note The year must be expressed as 4 digits. Decimals in date notation: Some countries specify dates with decimal points, as in {10.1.2008}, but WebDNA will interpret this as a time instead. You can force it to interpret text as a Date by inserting a "D" in front of the text, as in [math]{D10.01.1998}[/math].
sometimes you want to calculate something without displaying the results, perhaps while calculating a running total. To do this, put "show=F" into the math parameters, as in [Math show=F]total=total [subTotal][/Math]. This allows you to perform calculations in the middle of a web page without the intermediate numbers appearing to the visitor. Later, you can show the value of the math variable with [Math]total[/Math].
Some Examples:[math](4.5 6.2)/17*95-12[/math] | 47.7941176470588 |
[math]{4/7/1997} + 10[/math] | 729496 (4/7/1997 + 10 days expressed as number of days since 00/00/0000) |
[math]{4/7/1997} + {02/00/0000}[/math] | 729547 (4/7/1997 + 2 months expressed as days since 00/00/0000) |
[math date]{4/7/1997} + 10[/math] | 04/17/1997 (4/07/1997 + 10 days expressed as date) |
[math date]{4/7/1997} + {02/00/0000}[/math] | 06/07/1997 (4/17/1997 + 2 months expressed as date) |
[math date]{[date]}-{00/07/0000}[/math] | 09/01/2008 (One week ago today) |
[math]{12:51:02}[/math] | 46262 (number of seconds between midnight and 12:51:02 expressed as seconds) |
[math time]{12:51:02} + {01:00:05}[/math] | 13:51:07 (12:51:02 pm plus 1 hour and 5 seconds expressed as time) |
[math]x=5/3[/math] | 1.66666666666667 |
[math]x=5%3[/Math](% = Modulo Operator) | 2 |
<SELECT NAME="StartYear">[loop start=[date %Y]&end=[math][date %Y] + 10[/math]]<OPTION[showif [date %Y]=[index]] selected[/showif]>[index]</option>[/loop]</SELECT>Scientific Functions available in the Math contextExamples:
[math]ceil(1.5)[/math][math]sin([formvalue])*cos(3.1415)[/math]
Function | Description |
---|---|
sin(x) | returns sine of x. |
cos(x) | returns cosine of x. |
tan(x) | returns tangent of x. |
asin(x) | returns arcsine of x. |
acos(x) | returns arccosine of x. |
atan(x) | returns arctangent of x. |
sinh(x) | returns hyperbolic sine of x. |
cosh(x) | returns hyperbolic cosine of x. |
tanh(x) | returns hyperbolic tangent of x. |
log(x) | return natural log of x. |
log10(x) | returns log base 10 of x. |
sqrt(x) | returns square root of x sqrt(16) = 4. |
floor(x) | rounds down to next-lower integer. floor(2.9) = 2. |
ceil(x) | rounds up to next-higher integer. ceil(3.1) = 4. |
abs(x) | returns absolute value of x. abs(-3.4) = 3.4. |
deg(x) | converts radians to degrees. |
rad(x) | converts degrees to radians. |
you may use [format] to convert an integer number to a date or time. Use [Format Days_To_Date] and [Format Seconds_To_Time] to convert integer numbers to their equivalent dates/times. As already noted above, the integer number represents the number of days since January 1, 0000 and for time, the number of seconds since midnight.
[Format Days_To_Date]729496[/Format] yields 4/17/1997[Format Seconds_To_Time]46262[/Format] yields 12:51:02[math] variable names allow 15 characters. There is no such limit for [text] variable names
[math ResultType]Equation[/math]the name of a math variable is limited to 15 alphanumeric characters, and must begin with a letter.
You can assign multiple math variables at the same time using a semicolon to separate the assignments.
Example: [math show=f]var1=1;var2=2[/math].
[lineitems]
[quantity], [ref], [price], [math][price]*[quantity][/math]
[/lineitems]
[math show=f]counter=0[/math]
[founditems]
[hideif [somecondition]=something]
[math]counter=counter 1[/math]. [firstname] [lastname]<br>
[/hideif]
[/founditems]
There were [counter] names found.
sometimes you want to calculate something without displaying the results, perhaps while calculating a running total. To do this, put "show=F" into the math parameters, as in [Math show=F]total=total [subTotal][/Math]. This allows you to perform calculations in the middle of a web page without the intermediate numbers appearing to the visitor. Later, you can show the value of the math variable with [math]total[/Math].
[math](4.5 6.2)/17*95-12[/math] | 47.7941176470588 |
[math]{4/7/1997} + 10[/math] | 729496 (4/7/1997 + 10 days expressed as number of days since 00/00/0000) |
[math]{4/7/1997} + {02/00/0000}[/math] | 729547 (4/7/1997 + 2 months expressed as days since 00/00/0000) |
[math date]{4/7/1997} + 10[/math] | 04/17/1997 (4/07/1997 + 10 days expressed as date) |
[math date]{4/7/1997} + {02/00/0000}[/math] | 06/07/1997 (4/17/1997 + 2 months expressed as date) |
[math date]{[date]}-{00/07/0000}[/math] | 09/01/2008 (One week ago today) |
[math]{12:51:02}[/math] | 46262 (number of seconds between midnight and 12:51:02 expressed as seconds) |
[math time]{12:51:02} + {01:00:05}[/math] | 13:51:07 (12:51:02 pm plus 1 hour and 5 seconds expressed as time) |
[math]x=5/3[/math] | 1.66666666666667 |
[math]x=5%3[/Math](% = Modulo Operator) | 2 |
<SELECT NAME="StartYear">
[loop start=[date %Y]&end=[math][date %Y] + 10[/math]]
<OPTION[showif [date %Y]=[index]] selected[/showif]>[index]</option>
[/loop]
</SELECT>
[math]ceil(1.5)[/math]
[math]sin([formvalue])*cos(3.1415)[/math]
Function | Description |
---|---|
sin(x) | returns sine of x. |
cos(x) | returns cosine of x. |
tan(x) | returns tangent of x. |
asin(x) | returns arcsine of x. |
acos(x) | returns arccosine of x. |
atan(x) | returns arctangent of x. |
sinh(x) | returns hyperbolic sine of x. |
cosh(x) | returns hyperbolic cosine of x. |
tanh(x) | returns hyperbolic tangent of x. |
log(x) | return natural log of x. |
log10(x) | returns log base 10 of x. |
sqrt(x) | returns square root of x sqrt(16) = 4. |
floor(x) | rounds down to next-lower integer. floor(2.9) = 2. |
ceil(x) | rounds up to next-higher integer. ceil(3.1) = 4. |
abs(x) | returns absolute value of x. abs(-3.4) = 3.4. |
deg(x) | converts radians to degrees. |
rad(x) | converts degrees to radians. |
you may use [format] to convert an integer number to a date or time. Use [Format Days_To_Date] and [Format Seconds_To_Time] to convert integer numbers to their equivalent dates/times. As already noted above, the integer number represents the number of days since January 1, 0000 and for time, the number of seconds since midnight.
[math] variable names allow 15 characters. There is no such limit for [text] variable names
Terry WilsonDOWNLOAD WEBDNA NOW!
A compilation of some user's questions...
Tips and TricksA list of user-submitted tips ...
AWS Raw WebDNA LAMP-Plus WebServerAmazon Web Services (AWS) README for Machine Image ID...
WebDNA ModulesA list of the currently available modules...
Download WebDNA ApplicationsWebDNA applications...
Technical Change HistoryThis Technical Change History provides a reverse chronological list of WebDNA changes...
When listing files...
[xsl]Enables the WebDNA programmer to compile and apply XSL style sheets to XML data...
[hideif][HideIf Comparison]Hide This HTML[/HideIf]...
[getchars]Extracts a portion of the text (also known as substring or Mid$)...
[if]This context displays HTML or executes WebDNA conditionally only if the expression is true...
[shownext][shownext] is a special context used to create links (or more accurately...