5 Replies Latest reply on Apr 16, 2013 4:45 PM by The Madness

# process calculation from text

Hi,

I there an easy way to process a calculation that is in text form as in:

"2567 + 82736"

"1000 * (27363 / 38374)"

"3434 + 34334  + 34332 + 83938"

Or do the strings have to be parsed and processed that way.  I was hoping there was a way to evaluate the calculation?

thanks.

• ###### 1. Re: process calculation from text

You can use LSParseNumber() around your strings to convert them to numeric values, then just add them.

• ###### 2. Re: process calculation from text

I don't think so.  The string is not a number, it's a formula.

He might be able to use evaluate().  Mind you, if you can put "1 + 1" into a textbox, you can also put in "fred flinstone".

• ###### 3. Re: process calculation from text

Dan Bracuk wrote:

I don't think so.  The string is not a number, it's a formula.

Good point.  I was thinking he'd wrap the individual numbers, not the entire expression, but I see what he's saying now.

• ###### 4. Re: process calculation from text

Thanks for responding guys.  Evaluate doesn't work (at least not the way I've tried it).  E.g.
<cfset val = 1>
<cfset vall = 2>
<cfset op = "+">
<cfset new_expr = "1 + 2">
#evaluate("new_expr")# resolves to "1 + 2"
#evaluate("val #op# vall")# resolves to "3"

So unless there are any other ideas, I guess I will have to parse.  You'd think there'd be an easier way :-(   Thanks again.

• ###### 5. Re: process calculation from text

Okay, I figured it out.  I needed to use evaluate(new_expr) not evaluate("new_expr").  It is working for me.  I used a cftry/catch to get any field that couldn't be resolved.  Thanks a lot!