KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Applying a Simple Calculator to A Database (v14R4+)
PRODUCT: 4D | VERSION: 14 R4 | PLATFORM: Mac & Win
Published On: February 17, 2015

The new 4DEVAL 4D Tags feature allows a simple calculator to easily be implemented to a database. Below is an example where the first Text Box takes in an equation and the second Text Box displays the result:



This is done simply by using the following code:

//expression_t is the text box that the user will input an expression in
//result_t is the result of the expression

C_TEXT($expression_tag)
$expression_tag:="<"+"!--#4deval String("+expression_t+")-->"
PROCESS 4D TAGS($expression_tag;result_t)


This takes advantage of the String function's ability to calculate an expression and convert it into a single numeric representation of the expression.

In the example the code is called from the "Evaluate" button, but can be implemented else where if needed.

It is also noted that the expressions are note evaluated with the correct order of operations and parenthesis must be used to provide precedence, if not the expression will be evaulated from left to right.
  For example:
   2+3+4/2=4.5
   2+3+(4/2)=7


This feature can be placed where simple mathematics are needed so that the user does not need to use external resoruces to calculate data.

Commented by Tai Bui on February 20, 2015 at 1:03 PM
Hi, There was a mistake, the last line should be: PROCESS 4D TAGS($expression_tag;result_t)
Commented by Keith Culotta on February 20, 2015 at 8:53 AM
I am new to PROCESS 4D TAGS. A syntax error is avoided when the last line above reads "PROCESS 4D TAGS($expression_tag;result_t)" However, the debuger shows that when: $expression_tag = the result_t also = Tested with: C_TEXT(expression_t;result_t;$expression_tag) expression_t:=Request("Test calc";expression_t) $expression_tag:="<" "!--#4deval String(" expression_t ")-->" PROCESS 4D TAGS($expression_tag;result_t)