## Solved:Need simple example,real time calculating and output the result

Get help with using AutoHotkey and its commands and hotkeys
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

### Re: Solved:Need simple example,real time calculating and output the result

Code: Select all

``````;-------------------------------------------------------------------------------
calc_Percent(Var) { ; replace 123% with 1.23 and similar
;-------------------------------------------------------------------------------
while RegExMatch(Var, "([\d\.]+)%", M )
Var := StrReplace(Var, M1 "%", M1 / 100)
return Var
}
``````
This small update should work
YoucefHam
Posts: 344
Joined: 24 Aug 2015, 12:56
GitHub: YoucefHam
Location: Algeria
Contact:

### Re: Solved:Need simple example,real time calculating and output the result

wolf_II wrote:
11 Jun 2019, 02:27
Thanks, YoucefHam for the code. This is a very good example how to handle unary operators. (or whatever it is, but I think that's right)
I trimmed the code a bit:
Thanks @wolf_II, I am a noob in RegEx codes.
There is always more than one way to solve a problem.
vsub
Posts: 470
Joined: 29 Sep 2015, 03:39

### Re: Solved:Need simple example,real time calculating and output the result

MannyKSoSo wrote:
11 Jun 2019, 10:06

Code: Select all

``````;-------------------------------------------------------------------------------
calc_Percent(Var) { ; replace 123% with 1.23 and similar
;-------------------------------------------------------------------------------
while RegExMatch(Var, "([\d\.]+)%", M )
Var := StrReplace(Var, M1 "%", M1 / 100)
return Var
}
``````
This small update should work
Thanks
Posts: 1738
Joined: 29 Mar 2015, 09:41
Contact:

### Re: Solved:Need simple example,real time calculating and output the result

Code: Select all

``````calc_Percent(Var) {
Return RegExReplace(Var, "\d\K%", "/100")
}``````
Why not like this?
vsub
Posts: 470
Joined: 29 Sep 2015, 03:39

### Re: Solved:Need simple example,real time calculating and output the result

Any mathematician here?
I think there is still something wrong...which one is the correct answer(I only know that 50% of 150 is 75 but I am not sure what symbol you have to use divide or multiply when calculating percentages)

150/50% = 300 - MannyKSoSo version,another calculator,google
150/50% = 75(clicking on the percentage it convert it to 75 which makes it 150/75) - the windows calculator
150/50% = 0.03 - teadrinker version

150*50% = 75 - everyone think that

150+50% = 225(clicking on the percentage it convert it to 75 which makes it 150+75)...windows and google

MannyKSoSo\teadrinker version - 150.5

150-50% - same result as with +
Posts: 1738
Joined: 29 Mar 2015, 09:41
Contact:

### Re: Solved:Need simple example,real time calculating and output the result

Code: Select all

``````MsgBox, % calc_Percent("150/50%")

calc_Percent(Var) {
Return RegExReplace(Var, "(\d+(\.\d+)?)%", "(\$1/100)")
}``````
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

### Re: Solved:Need simple example,real time calculating and output the result

150/50% what this is doing normally is saying 150/.5 (normal functionality)
when you click the % sign on the calculator it calculates what the percentage would be, ie 150/75. Both results are correct, but in terms of what it is asking for 150/.5 is proper.

Note: I am not a math major.
YoucefHam
Posts: 344
Joined: 24 Aug 2015, 12:56
GitHub: YoucefHam
Location: Algeria
Contact:

### Re: Solved:Need simple example,real time calculating and output the result

"150*50%" ==> give 50% of 150 = 75
"150/50%" ==> give 1/50% = 200% of 150 = 300

"150+50%" ==> add (50% of 150 = 75) to 150 = 225
"150*150%" ==> same as above add (50% of 150 = 75) to 150 = 225

"150-50%" ==> sub (50% of 150 = 75) to 150 = 75

Code: Select all

``````MsgBox, % calc_Percent( "25.2%" )
MsgBox, % calc_Percent( "((5+15)*4)/20%+20+3%" )
MsgBox, % (calc_Percent( "150+50%" ))
MsgBox, % (calc_Percent( "150-50%" ))
MsgBox, % (calc_Percent( "150-50%+2%" ))  ;cal 2% of 50% of 150 = 75*2% = 1.5

;-------------------------------------------------------------------------------
calc_Percent(Var) { ; replace 123% with 1.23 and similar
;-------------------------------------------------------------------------------
while RegExMatch(Var, "/([\d\.]+)%", M ) or RegExMatch(Var, "\*([\d\.]+)%", M )
Var := StrReplace(Var, M1 "%", M1 / 100)
while RegExMatch(Var, "([\d\.]+)(\+)([\d\.]+)%", M ) or RegExMatch(Var, "([\d\.]+)(-)([\d\.]+)%", M )
Var := StrReplace(Var, M1 M2 M3 "%", M1 M2 (M1 * M3 / 100))
return Var
}``````
There is always more than one way to solve a problem.
Rohwedder
Posts: 3269
Joined: 04 Jun 2014, 08:33
Location: Germany

### Re: Solved:Need simple example,real time calculating and output the result

Hallo, YoucefHam,
there still seems to be a bug in there:

Code: Select all

``````MsgBox, % calc_Percent( "25.2%" )	;25.2%
MsgBox, % calc_Percent( "((5+15)*4)/20%+20+3%" ) ;((5+15)*4)/0.200000+20+0.600000
MsgBox, % (calc_Percent( "150+50%" )) ;150+75.000000
MsgBox, % (calc_Percent( "150-50%" )) ;150-75.000000
MsgBox, % (calc_Percent( "150-50%+2%" )) ;150-75.000000+1.500000
;cal 2% of 50% of 150 = 75*2% = 1.5

;-------------------------------------------------------------------------------
calc_Percent(Var) { ; replace 123% with 1.23 and similar
;-------------------------------------------------------------------------------
while RegExMatch(Var, "/([\d\.]+)%", M ) or RegExMatch(Var, "\*([\d\.]+)%", M )
Var := StrReplace(Var, M1 "%", M1 / 100)
while RegExMatch(Var, "([\d\.]+)(\+)([\d\.]+)%", M ) or RegExMatch(Var, "([\d\.]+)(-)([\d\.]+)%", M )
Var := StrReplace(Var, M1 M2 M3 "%", M1 M2 (M1 * M3 / 100))
return Var
}``````
YoucefHam
Posts: 344
Joined: 24 Aug 2015, 12:56
GitHub: YoucefHam
Location: Algeria
Contact:

### Re: Solved:Need simple example,real time calculating and output the result

How !!!
There is always more than one way to solve a problem.
Rohwedder
Posts: 3269
Joined: 04 Jun 2014, 08:33
Location: Germany

### Re: Solved:Need simple example,real time calculating and output the result

YoucefHam
Posts: 344
Joined: 24 Aug 2015, 12:56
GitHub: YoucefHam
Location: Algeria
Contact:

### Re: Solved:Need simple example,real time calculating and output the result

xd sorry my bad, I focused on the other and removed the first one.

here You good Now.

Code: Select all

``````MsgBox, % calc_Percent( "125.2%" )
MsgBox, % calc_Percent( "((5+15)*4)/20%+20+3%" )
MsgBox, % (calc_Percent( "150+50%" ))
MsgBox, % (calc_Percent( "150-50%" ))
MsgBox, % (calc_Percent( "150-50%+2%" ))  ;cal 2% of 50% of 150 = 75*2% = 1.5

;-------------------------------------------------------------------------------
calc_Percent(Var) {
;-------------------------------------------------------------------------------
while RegExMatch(Var, "/([\d\.]+)%", M ) or RegExMatch(Var, "\*([\d\.]+)%", M )
Var := StrReplace(Var, M1 "%", M1 / 100)
while RegExMatch(Var, "([\d\.]+)(\+)([\d\.]+)%", M ) or RegExMatch(Var, "([\d\.]+)(-)([\d\.]+)%", M )
Var := StrReplace(Var, M1 M2 M3 "%", M1 M2 (M1 * M3 / 100))
while RegExMatch(Var, "([\d\.]+)%", M )
Var := StrReplace(Var, M1 "%", M1 / 100)
return Var
}``````
There is always more than one way to solve a problem.
Rohwedder
Posts: 3269
Joined: 04 Jun 2014, 08:33
Location: Germany

### Re: Solved:Need simple example,real time calculating and output the result

Sorry!
not yet good enough.

Code: Select all

``````MsgBox, % calc_Percent( "125.2%" ) ;1.252000 OK
MsgBox, % calc_Percent( "((5+15)*4)/20%+20+3%" ) ;((5+15)*4)/0.200000+20+0.600000 wrong!
MsgBox, % (calc_Percent( "150+50%" )) ;150+75.000000 wrong!
MsgBox, % (calc_Percent( "150-50%" )) ;150-75.000000 wrong!
MsgBox, % (calc_Percent( "150-50%+2%" ))  ;150-75.000000+1.500000 wrong!
;cal 2% of 50% of 150 = 75*2% = 1.5

;-------------------------------------------------------------------------------
calc_Percent(Var) {
;-------------------------------------------------------------------------------
while RegExMatch(Var, "/([\d\.]+)%", M ) or RegExMatch(Var, "\*([\d\.]+)%", M )
Var := StrReplace(Var, M1 "%", M1 / 100)
while RegExMatch(Var, "([\d\.]+)(\+)([\d\.]+)%", M ) or RegExMatch(Var, "([\d\.]+)(-)([\d\.]+)%", M )
Var := StrReplace(Var, M1 M2 M3 "%", M1 M2 (M1 * M3 / 100))
while RegExMatch(Var, "([\d\.]+)%", M )
Var := StrReplace(Var, M1 "%", M1 / 100)
return Var
}``````
It works better this way:

Code: Select all

``````MsgBox, % calc_Percent( "125.2%" ) ;1.252000
MsgBox, % calc_Percent( "((5+15)*4)/20%+20+3%" ) ;((5+15)*4)/0.200000+20+0.030000
MsgBox, % calc_Percent( "150+50%" ) ;150+0.500000
MsgBox, % calc_Percent( "150-50%" ) ;150-0.500000
MsgBox, % calc_Percent( "150-50%+2%" ) ;150-0.500000+0.020000
;-------------------------------------------------------------------------------
calc_Percent(Var) {
;-------------------------------------------------------------------------------
while RegExMatch(Var, "[/|\*]([\d\.]+)%", M)
Var := StrReplace(Var, M1 "%", M1 / 100)
while RegExMatch(Var, "([\d\.]+)(\+|-)([\d\.]+)%", M)
Var := StrReplace(Var, M1 M2 M3 "%", M1 M2 (M3 / 100))
while RegExMatch(Var, "([\d\.]+)%", M)
Var := StrReplace(Var, M1 "%", M1 / 100)
return Var
}``````
vsub
Posts: 470
Joined: 29 Sep 2015, 03:39

### Re: Solved:Need simple example,real time calculating and output the result

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
}``````
Rohwedder
Posts: 3269
Joined: 04 Jun 2014, 08:33
Location: Germany

### Re: Solved:Need simple example,real time calculating and output the result

The code was made by YoucefHam.
I only removed one bug.
Unfortunately, both still make mistakes!
vsub-mistake: No1
Youcef-mistake: No4

Code: Select all

``````MsgBox, % "No1" calc_Percent( "125.2%+.5%" )
MsgBox, % "No2" calc_Percent( "((5+15)*4)/20%+20+3%" )
MsgBox, % "No3" calc_Percent( "150/50%" )
MsgBox, % "No4" calc_Percent( "1.50%/50%" )
MsgBox, % "No5" calc_Percent( "150%-0.50%+2%" )

calc_Percent(Exp)
{
Return, "`t`t" Exp "`nvsub:`t`t" calc1_Percent(Exp) "`nYoucefHam:`t" calc2_Percent(Exp)
}

calc1_Percent(Exp)
{
Return, RegExReplace(EXP, "(\d+(\.\d+)?)%", "(\$1/100)")
}
;-------------------------------------------------------------------------------
calc2_Percent(Var) {
;-------------------------------------------------------------------------------
while RegExMatch(Var, "[/|\*]([\d\.]+)%", M)
Var := StrReplace(Var, M1 "%", M1 / 100)
while RegExMatch(Var, "([\d\.]+)(\+|-)([\d\.]+)%", M)
Var := StrReplace(Var, M1 M2 M3 "%", M1 M2 (M3 / 100))
while RegExMatch(Var, "([\d\.]+)%", M)
Var := StrReplace(Var, M1 "%", M1 / 100)
return Var
}``````
YoucefHam
Posts: 344
Joined: 24 Aug 2015, 12:56
GitHub: YoucefHam
Location: Algeria
Contact:

### Re: Solved:Need simple example,real time calculating and output the result

try this

Code: Select all

``````MsgBox, % "No1" calc_Percent( "125.2%+0.5%" )
MsgBox, % "No2" calc_Percent( "((5+15)*4)/20%+20+3%" )
MsgBox, % "No3" calc_Percent( "150/50%" )
MsgBox, % "No4" calc_Percent( "150%/50%" )
MsgBox, % "No5" calc_Percent( "150%-0.50%+2%" )

;~ Loop
;~ {
;~ InputBox, var
;~ if !ErrorLevel
;~ MsgBox, % calc_Percent(var)
;~ else
;~ ExitApp
;~ }
;~ return

calc_Percent(Exp)
{
Return, "`t`t" Exp "`nvsub:`t`t" calc1_Percent(Exp) "`nYoucefHam:`t" calc2_Percent(Exp)
}

calc1_Percent(Exp)
{
Return, RegExReplace(EXP, "(\d+(\.\d+)?)%", "(\$1/100)")
}
;-------------------------------------------------------------------------------
calc2_Percent(Var) {
;-------------------------------------------------------------------------------
while InStr( Var, "%")
{
if RegExMatch(Var, "(\d*\.*\d+)(\+|-)(\d*\.*\d+)%", M)
Var := StrReplace(Var, M1 M2 M3 "%", M1 M2 (M1 * M3 / 100))
else if RegExMatch(Var, "(\d*\.*\d+)%", M)
Var := StrReplace(Var, M1 "%", M1 / 100)
ToolTip, % var, 0, 0
}
return Var
}``````
There is always more than one way to solve a problem.
Rohwedder
Posts: 3269
Joined: 04 Jun 2014, 08:33
Location: Germany

### Re: Solved:Need simple example,real time calculating and output the result

I didn't think there was any interest left, so I corrected the code of vsub only for myself.
This code seems to be correct now, but (sorry) your new suggestion makes mistakes at No1, 2, 5 and 6:

Code: Select all

``````MsgBox, % "No1" calc_Percent( "125.2%+0.5%" )
MsgBox, % "No2" calc_Percent( "((5+15)*4)/20%+20+3%" )
MsgBox, % "No3" calc_Percent( "150/50%" )
MsgBox, % "No4" calc_Percent( "150%/50%" )
MsgBox, % "No5" calc_Percent( "150%-0.50%+2%" )
MsgBox, % "No6" calc_Percent( "15.2%-.5%/30%" )
MsgBox, % "No7" calc_Percent( ".2%*.5%/30%" )

;~ Loop
;~ {
;~ InputBox, var
;~ if !ErrorLevel
;~ MsgBox, % calc_Percent(var)
;~ else
;~ ExitApp
;~ }
;~ return

calc_Percent(Exp)
{
Return, "`t`t" Exp "`nvsub(corrected):`t" calc1_Percent(Exp) "`nYoucefHam:`t" calc2_Percent(Exp)
}

calc1_Percent(Exp)
{
Return, RegExReplace(EXP, "(\d*(\.\d+)?)%", "(\$1/100)")
}
;-------------------------------------------------------------------------------
calc2_Percent(Var) {
;-------------------------------------------------------------------------------
while InStr( Var, "%")
{
if RegExMatch(Var, "(\d*\.*\d+)(\+|-)(\d*\.*\d+)%", M)
Var := StrReplace(Var, M1 M2 M3 "%", M1 M2 (M1 * M3 / 100))
else if RegExMatch(Var, "(\d*\.*\d+)%", M)
Var := StrReplace(Var, M1 "%", M1 / 100)
ToolTip, % var, 0, 0
}
return Var
}``````
YoucefHam
Posts: 344
Joined: 24 Aug 2015, 12:56
GitHub: YoucefHam
Location: Algeria
Contact:

### Re: Solved:Need simple example,real time calculating and output the result

nop I am not mistaking, use any calculator it will give you the same result.

There is always more than one way to solve a problem.
YoucefHam
Posts: 344
Joined: 24 Aug 2015, 12:56
GitHub: YoucefHam
Location: Algeria
Contact:

### Re: Solved:Need simple example,real time calculating and output the result

if you want it your way here is the code

Code: Select all

``````MsgBox, % "No1" calc_Percent( "125.2%+0.5%" )
MsgBox, % "No2" calc_Percent( "((5+15)*4)/20%+20+3%" )
MsgBox, % "No3" calc_Percent( "150/50%" )
MsgBox, % "No4" calc_Percent( "150%/50%" )
MsgBox, % "No5" calc_Percent( "150%-0.50%+2%" )
MsgBox, % "No6" calc_Percent( "15.2%-.5%/30%" )
MsgBox, % "No7" calc_Percent( ".2%*.5%/30%" )

;~ Loop
;~ {
;~ InputBox, var
;~ if !ErrorLevel
;~ MsgBox, % calc_Percent(var)
;~ else
;~ ExitApp
;~ }
;~ return

calc_Percent(Exp)
{
Return, "`t`t" Exp "`nvsub(corrected):`t" calc1_Percent(Exp) "`nYoucefHam:`t" calc2_Percent(Exp)
}

calc1_Percent(Exp)
{
Return, RegExReplace(EXP, "(\d*(\.\d+)?)%", "(\$1/100)")
}
;-------------------------------------------------------------------------------
calc2_Percent(Var) {
;-------------------------------------------------------------------------------
while RegExMatch(Var, "(\d*\.*\d+)%", M)
Var := StrReplace(Var, M1 "%", M1 / 100)
return Var
}``````
There is always more than one way to solve a problem.
Rohwedder
Posts: 3269
Joined: 04 Jun 2014, 08:33
Location: Germany

### Re: Solved:Need simple example,real time calculating and output the result

Hallo,
I know what you did. Your code makes "percentage automatic calculation". That means:
You're typing: 80 + 2 % = then the calculator automatically solves the equation: 80 + 80 * 2% = 81.6
But the equation: 80 + 2% simply means: 80 + 2/100 = 80 + 0.02 = 80.02
There is a difference between an equation and what you have to type in the calculator to solve the equation!