Calculate a mathematical formula in a field?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
- 
				rumplestiltskin
- Posts: 223
- Joined: Wed Jun 21, 2006 7:33 pm
- Contact:
Calculate a mathematical formula in a field?
There is either a simple command I'm unable to find or I'm trying to find a simple solution that doesn't exist.
I have a field which will contain a mathematical formula, something like this:
5*5/2
Let's call the field "dataEntered". So how do I calculate the results of the field? Do I need to parse through it or is there a simple command like:
do field "dataEntered" (that didn't work)
the result of field "dataEntered" (that didn't work either)
Thanks,
Barry
			
			
									
									
						I have a field which will contain a mathematical formula, something like this:
5*5/2
Let's call the field "dataEntered". So how do I calculate the results of the field? Do I need to parse through it or is there a simple command like:
do field "dataEntered" (that didn't work)
the result of field "dataEntered" (that didn't work either)
Thanks,
Barry
Re: Calculate a mathematical formula in a field?
Hi Barry,
VALUE is your friend:
...
put value(fld "the one with the math in it")
...
  
 
Best
Klaus
			
			
									
									
						VALUE is your friend:
...
put value(fld "the one with the math in it")
...
 
 Best
Klaus
- 
				rumplestiltskin
- Posts: 223
- Joined: Wed Jun 21, 2006 7:33 pm
- Contact:
Re: Calculate a mathematical formula in a field?
Thank you, Klaus. Value may be my friend but you are, as well.
Barry
			
			
									
									
						Barry
Re: Calculate a mathematical formula in a field?
What Klaus said.
Another friend is that the value function obeys PEMDAS pretty well, but I would be careful anyway and use parentheses everywhere.
Craig Newman
			
			
									
									
						Another friend is that the value function obeys PEMDAS pretty well, but I would be careful anyway and use parentheses everywhere.
Craig Newman
- 
				rumplestiltskin
- Posts: 223
- Joined: Wed Jun 21, 2006 7:33 pm
- Contact:
Re: Calculate a mathematical formula in a field?
Exactly what I tell my Excel students at the community college; helps make the formula more understandable when you go back to it a month later.dunbarx wrote:...Another friend is that the value function obeys PEMDAS pretty well, but I would be careful anyway and use parentheses everywhere.

Re: Calculate a mathematical formula in a field?
Please enlight the unknowing, what is PEMDAS?
			
			
									
									
						Re: Calculate a mathematical formula in a field?
Parenthesis
Exponents
Multiply
Divide
Add
Subtract
Order of operations mnemonic.
			
			
									
									
						Exponents
Multiply
Divide
Add
Subtract
Order of operations mnemonic.
Klaus wrote:Please enlight the unknowing, what is PEMDAS?
Re: Calculate a mathematical formula in a field?
Ah, thanks, never heard before 
			
			
									
									
						
- 
				rumplestiltskin
- Posts: 223
- Joined: Wed Jun 21, 2006 7:33 pm
- Contact:
Re: Calculate a mathematical formula in a field?
We learned (in 2nd grade, IIRC) PEMDAS as "Please Excuse My Dear Aunt Sally" so it would be easy to remember. I find many of my freshman college students either don't remember it or never learned it so, when I teach them Excel, it's all new to them.
			
			
									
									
						Re: Calculate a mathematical formula in a field?
Hi Barry!
I have just released rGrid which is a spread sheet inspired grid control. One of its features is that it handles formulas written into the cells (fields).
rGrid is open source so you can use the code as you wish for non commercial projects. Here below is the code that parses the formulas with cell referencing. Hope it can be of use to you!
You can download the latest version of rGrid from http://www.tapirsoft.com
With my best regards
Mats Wilstrand
			
			
									
									I have just released rGrid which is a spread sheet inspired grid control. One of its features is that it handles formulas written into the cells (fields).
rGrid is open source so you can use the code as you wish for non commercial projects. Here below is the code that parses the formulas with cell referencing. Hope it can be of use to you!
Code: Select all
   put sGridDataA[tActiveSheet][tRowID][tColumnID]["formula"] into tCellRefs
   if tCellRefs is not empty then
      if char 1 of tCellRefs is not "=" then put "=" before sGridDataA[tActiveSheet][tRowID][tColumnID]["formula"]
      repeat for each item tItem in "=,+,-,/,*,^,<,>,sqrt,(,),if,then,put,sum,exp"
         replace tItem with cr in tCellRefs
      end repeat
      replace "$" with empty in tCellRefs
      put sGridDataA[tActiveSheet][tRowID][tColumnID]["formula"] into tValueFormula
      put empty into tTempCellRef
      repeat for each line tCellRef in tCellRefs
         if tCellRef is empty then next repeat
         switch
            case ":" is in tCellRef
               replace space with empty in tCellRef
               replace cr with empty in tCellRef
               put rangeToCells(tCellRef) into tRange
               replace ";" with comma in tRange
               replace tCellRef with tRange in tValueFormula
               put cr & rangeToCells(tCellRefs) after tTempCellRef
               break
            default
               put cr & tCellRef after tTempCellRef
               break
         end switch
      end repeat
      if char 1 of tTempCellRef is cr then delete char 1 of tTempCellRef
      replace ";" with cr in tTempCellRef
      repeat for each line tCellRef in tTempCellRef
         if tCellRef is empty then next repeat
         if tCellRef is a number then next repeat # Let numbers be calculated in formula
         put sGridDataA[tActiveSheet][item 1 of cellRefToID(tCellRef)][item 2 of cellRefToID(tCellRef)]["value"] into tCellValue
         if tCellValue is empty then put 0 into tCellValue
         if tCellValue is not a number then put false into tCanCalculate
         replace tCellRef with tCellValue in tValueFormula
      end repeat
      if char 1 of tValueFormula is "=" then delete char 1 of tValueFormula
      if tCanCalculate is not false
      then
         set the htmltext of control "CellField" of control id tCellContainerID of grp id tRowContainerID of me to value(tValueFormula)
         put value(tValueFormula) into sGridDataA[tActiveSheet][tRowID][tColumnID]["value"]
      else
         set the htmltext of control "CellField" of control id tCellContainerID of grp id tRowContainerID of me to "#VALUE!"
         put "#VALUE!" into sGridDataA[tActiveSheet][tRowID][tColumnID]["value"]
      end if
   end ifWith my best regards
Mats Wilstrand
http:www.tapirsoft.on-rev.com
Open Source LiveCode Plugins - rIDE, rGrid, rTree
LiveCode projects
						Open Source LiveCode Plugins - rIDE, rGrid, rTree
LiveCode projects
- 
				rumplestiltskin
- Posts: 223
- Joined: Wed Jun 21, 2006 7:33 pm
- Contact:
Re: Calculate a mathematical formula in a field?
Mats,
rGrid is v-e-r-y interesting. More than I need for the small project I am building right now but I have bookmarked the page so I can check it out later.
Thanks,
Barry
			
			
									
									
						rGrid is v-e-r-y interesting. More than I need for the small project I am building right now but I have bookmarked the page so I can check it out later.
Thanks,
Barry

