math function

how to get the result of a formula like ‘23 +1’ stored in a text var, in a dec var. I tried to use evaluate, but it doesn’t work : var MyText text MyDec decimal Code MyText := '23+1’; EVALUATE(MyDec,MyText); Any Idea ?

I’m not sure if there is a simple answer to this. I solved it with the coding below (which doesn’t do any error handling). Note this will calculate the sum from left to right. VAR j : Integer; // Simple loop variable i : Integer; // Simple loop variable NumOps : Integer; // Number of operators in string SumText : Text[30]; // String to calculate ConvText : Text[30]; // String with operators changed to # MyDec : Decimal; // Result MyNums : ARRAY [30] OF Decimal; // Array of the numbers MyOps : ARRAY [30] OF Text[2]; // Array of the operators Ptr1 : Integer; // Pointer variable Ptr2 : Integer; // Pointer variable BEGIN SumText := ‘23+1’; // Original Sum ConvText := CONVERTSTR(SumText,'±/’,’####’); // Replace operators with commas NumOps := STRLEN(ConvText) - STRLEN(DELCHR(ConvText,’=’,’#’)); // Count number of operators // Get Numbers and operators from string Ptr1 := 1; Ptr2 := 1; i := 1; REPEAT Ptr2 := STRPOS(COPYSTR(ConvText,Ptr1),’#’); // Find next separator IF (Ptr2 <> 0) THEN BEGIN MyOps[i] := COPYSTR(SumText,Ptr1+Ptr2-1,1); // Get next operator EVALUATE(MyNums[i],COPYSTR(COPYSTR(SumText,Ptr1),1,Ptr2-1)) // Get next number END ELSE BEGIN EVALUATE(MyNums[i],COPYSTR(COPYSTR(SumText,Ptr1),1)); // Get last number END; Ptr1 := Ptr1 + Ptr2; // Move pointer on i := i + 1; UNTIL (Ptr2 = 0); // Calculate sum from left to right MyDec := MyNums[1]; // Start with first number FOR j := 1 TO (i-1) DO // Move through sum and apply operator to next number BEGIN CASE (MyOps[j]) OF ‘+’ : MyDec := MyDec + MyNums[j + 1]; ‘-’ : MyDec := MyDec - MyNums[j + 1]; ‘*’ : MyDec := MyDec * MyNums[j + 1]; ‘/’ : MyDec := MyDec / MyNums[j + 1]; END; END; MESSAGE(‘Answer is %1’,MyDec); END;

quote:


Originally posted by PaulBradbury: I’m not sure if there is a simple answer to this. I solved it with the coding below (which doesn’t do any error handling). Note this will calculate the sum from left to right. VAR j : Integer; // Simple loop variable i : Integer; // Simple loop variable NumOps : Integer; // Number of operators in string SumText : Text[30]; // String to calculate ConvText : Text[30]; // String with operators changed to # MyDec : Decimal; // Result MyNums : ARRAY [30] OF Decimal; // Array of the numbers MyOps : ARRAY [30] OF Text[2]; // Array of the operators Ptr1 : Integer; // Pointer variable Ptr2 : Integer; // Pointer variable BEGIN SumText := ‘23+1’; // Original Sum ConvText := CONVERTSTR(SumText,'±/’,’####’); // Replace operators with commas NumOps := STRLEN(ConvText) - STRLEN(DELCHR(ConvText,’=’,’#’)); // Count number of operators // Get Numbers and operators from string Ptr1 := 1; Ptr2 := 1; i := 1; REPEAT Ptr2 := STRPOS(COPYSTR(ConvText,Ptr1),’#’); // Find next separator IF (Ptr2 <> 0) THEN BEGIN MyOps[i] := COPYSTR(SumText,Ptr1+Ptr2-1,1); // Get next operator EVALUATE(MyNums[i],COPYSTR(COPYSTR(SumText,Ptr1),1,Ptr2-1)) // Get next number END ELSE BEGIN EVALUATE(MyNums[i],COPYSTR(COPYSTR(SumText,Ptr1),1)); // Get last number END; Ptr1 := Ptr1 + Ptr2; // Move pointer on i := i + 1; UNTIL (Ptr2 = 0); // Calculate sum from left to right MyDec := MyNums[1]; // Start with first number FOR j := 1 TO (i-1) DO // Move through sum and apply operator to next number BEGIN CASE (MyOps[j]) OF ‘+’ : MyDec := MyDec + MyNums[j + 1]; ‘-’ : MyDec := MyDec - MyNums[j + 1]; ‘*’ : MyDec := MyDec * MyNums[j + 1]; ‘/’ : MyDec := MyDec / MyNums[j + 1]; END; END; MESSAGE(‘Answer is %1’,MyDec); END;


thanks for this code, i’ll try it

The best you can do is create your own “Calculator” codeunit/function, checking for errors in the values entered (example: ‘3++’ should be incorrect, but also ‘3±2’ can be correct…) and also checking the brackets, as is not the same 2+(3*4) than (2+3)*4… I made a codeunit for doing those operations (and a some more a bit more complex some time ago…), but i’ven’t got the object here, sorry… Alfonso Pertierra (Spain)apertierra@teleline.es