Maybe I miss the point here but...
Rohwedder from your last example I get the result of the calculations you posted by just using
Var := RegExReplace(Var, "(\d+(\.\d+)?)%", "($1/100)")
MsgBox, % calc_Percent( "125.2%" ) ;1.252000
I get (125.2/100) and when I do the calculation I get the same end result.
MsgBox, % calc_Percent( "((5+15)*4)/20%+20+3%" ) ;((5+15)*4)/0.200000+20+0.030000
I get ((5+15)*4)/(20/100)+20+(3/100) and when I do the calculation I get the same end result.
MsgBox, % calc_Percent( "150+50%" ) ;150+0.500000
I get 150+(50/100) and when I do the calculation I get the same end result.
MsgBox, % calc_Percent( "150-50%+2%" ) ;150-0.500000+0.020000
I get 150-(50/100)+(2/100) and when I do the calculation I get the same end result.
Code: Select all
;-------------------------------------------------------------------------------
Eval(Exp) { ; using Javascript/COM
;-------------------------------------------------------------------------------
static Constants := "E|LN2|LN10|LOG2E|LOG10E|PI|SQRT1_2|SQRT2"
static Functions := "abs|acos|asin|atan|atan2|ceil|cos|exp|floor"
. "|log|max|min|pow|random|round|sin|sqrt|tan"
; while RegExMatch(Exp, "([\d\.]+)%", M )
; Exp := StrReplace(Exp, M1 "%", M1 / 100)
EXP := RegExReplace(EXP, "(\d+(\.\d+)?)%", "($1/100)")
Transform, Exp, Deref, %Exp% ; support variables
Exp := Format("{:L}", Exp) ; everything lowercase
Exp := RegExReplace(Exp, "i)(" Constants ")", "$U1") ; constants uppercase
Exp := RegExReplace(Exp, "i)(" Constants "|" Functions ")", "Math.$1")
Exp := StrReplace(Exp, "Math.ceil", "Math.ceil")
Exp := StrReplace(Exp, "Math.exp", "Math.exp")
Obj := ComObjCreate("HTMLfile")
Obj.Write("<body><script>document.body.innerText=eval('" Exp "');</script>")
return, InStr(Result := Obj.body.innerText, "body") ? "ERROR" : Result
}