simple + math

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sammy4
Posts: 16
Joined: 28 Sep 2020, 00:28

simple + math

22 Jul 2021, 21:44

Hey,
How can i do infinite addition if i press ^c again? at the moment i can only add up 2 numbers.

Code: Select all

amount =  
Clipboard = 
send, {ctrl up} 
~rbutton::

Send ^c                          

amount := Clipboard
sleep, 10
amount2 := Clipboard + amount

return

a::

msgbox, %amount2%
return

esc::exitapp 

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: simple + math

22 Jul 2021, 22:05

Code: Select all

sum = 0
a::MsgBox, 64, Sum, %sum%
RButton::
Clipboard =
Send ^c
ClipWait, 0
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Else sum += Clipboard
Return
^c is not the hotkey. It is RButton.
sammy4
Posts: 16
Joined: 28 Sep 2020, 00:28

Re: simple + math

22 Jul 2021, 23:11

lol, i forgot the rbutton hotkey was still a thing...no wonder the edits didn't change anything.. anyways, thank you so much it works like a charm :thumbup:
sammy4
Posts: 16
Joined: 28 Sep 2020, 00:28

Re: simple + math

23 Jul 2021, 00:40

one last question, how can msgbox show decimals?

e.g.

104,112.297 + 5,035.332 = 109,147.629

thanks.
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: simple + math

23 Jul 2021, 06:39

Although AHK will not do math with commas, you can remove the commas.

Code: Select all

sum += StrReplace(Clipboard, ",")
Rohwedder
Posts: 7608
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: simple + math

23 Jul 2021, 12:10

Hallo,
perhaps:

Code: Select all

a = 104112.297
b = 5035.332

MsgBox,% ThSep(a) " + " ThSep(b) " = " ThSep(a + b)

ThSep(x, s=",")
{ ; s = thousands separator
	x := RegExReplace(x, "\.0*$|\.\d*?\K0+$")
	Return, RegExReplace(x, "\G\d+?(?=(\d{3})+(?:\D|$))", "$0" s)
}
sammy4
Posts: 16
Joined: 28 Sep 2020, 00:28

Re: simple + math

23 Jul 2021, 13:03

thanks so much guys
sammy4
Posts: 16
Joined: 28 Sep 2020, 00:28

Re: simple + math

23 Jul 2021, 13:25

for some odd reason it adds random numbers to the copied value.
104,112.297 turns into 104216.297000

Code: Select all

sum = 0
~a::MsgBox, 64, Sum, % sum
mButton::
Clipboard =
Send ^c
ClipWait, 0

If ErrorLevel
{
  sleep, 1
}
Else
{
sum += StrReplace(Clipboard, ",")
sleep, 1
sum += Clipboard
} 
Return
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: simple + math

23 Jul 2021, 13:39

Instead of adding to the sum twice, just add once.
sammy4
Posts: 16
Joined: 28 Sep 2020, 00:28

Re: simple + math

23 Jul 2021, 14:18

i see, i thought "sum += StrReplace(Clipboard, ",")" would only replace the ","

have a good day.
swub
Posts: 19
Joined: 25 Feb 2019, 09:16

Re: simple + math

26 Jul 2022, 14:59

mikeyww wrote:
23 Jul 2021, 06:39
Although AHK will not do math with commas, you can remove the commas.

Code: Select all

sum += StrReplace(Clipboard, ",")
How could you prove that AHK knows how to handle commas in math? I have a scenario where I get the StatusBarGetText from Notepad++ to grab the Line # but have problems whenever line is over 1000 I think because it is reading as 1,000. Thanks for ALL your help! You are part of what makes AutoHotkey so GREAT! swub :superhappy:
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: simple + math

26 Jul 2022, 15:06

Kind of you! :)
swub
Posts: 19
Joined: 25 Feb 2019, 09:16

Re: simple + math

26 Jul 2022, 15:42

Code: Select all

vTest := 1,000
vTestOut := 26
EnvSub, vTest, %vTestout%
MsgBox %vTest%
And the MsgBox says "-25" So can you show me how it should be done?

Thank You again!
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: simple + math

26 Jul 2022, 16:37

Code: Select all

vTest    := "1,000"
vTestOut := "26"
MsgBox, 64, Difference, % vTest := subtractStrings(vTest, vTestOut)

subtractStrings(str1, str2) {
 Return StrReplace(str1, ",") - StrReplace(str2, ",")
}
A demonstration:

Code: Select all

vTest1 := 1,000
vTest2 := 3,4,5,6,7,8,9
MsgBox, %vTest1%
MsgBox, %vTest2%
Explained: https://www.autohotkey.com/docs/commands/SetExpression.htm

A string with commas is not interpreted as a number.
An expression with commas is interpreted as multiple expressions (I believe).
My best explanation of this is that the value of an expression containing commas is equal to the value of the first expression in the list. Others may have a more technically accurate description.

Code: Select all

vTest2 := (3,4,5,6,7,8,9)
MsgBox, %vTest2%
Another demonstration of expressions:

Code: Select all

Send % (3, c := 5, a := "9")
MsgBox, %c%
MsgBox, %a%
The answer to your question is straightforward and is the same answer previously provided in this thread: you remove the commas before you introduce the mathematics.

And now for the final exam, to see whether you learned something. :)

Code: Select all

456 = 1,000
3 = 456
MsgBox, %3%
swub
Posts: 19
Joined: 25 Feb 2019, 09:16

Re: simple + math

27 Jul 2022, 07:30

Thank you so much for explaining this to me. Sometimes my ADD gets in the way and your explanation made it very clear. OH and the answer to the test is: 456. :bravo:
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: simple + math

27 Jul 2022, 11:34

Class participation: A+
Coding: A-
Overall: A

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bobak, jaka1, MrDoge and 237 guests