## Help with sum and dividing numbers? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Krd
Posts: 52
Joined: 10 Mar 2020, 02:46

### Help with sum and dividing numbers?

Hello folks.

14 374,00 21 487,00 22 460,00 21 562,00

Those are my some random numbers (Currency). Here sum would be 79 883,00 and divided by 4 would be 19970,75.

If I higlght and copy them to clicpboard, how can AHK take over and devide them for me according to how many numbers/blocks which are seperetaed by a space? Some regex magic?

The numbers would be all from 1 to 12. It could be 14 374,00 22 460,00 21 562,00 or 14 374,00 0,00 22 460,00 21 562,00 21 487,00 22 460,00 21 562,00

The trick is to find what to devide it with.

Thank you
flyingDman
Posts: 758
Joined: 29 Sep 2013, 19:01

### Re: Help with sum and dividing numbers?

Create an array from these numbers using strsplit and divide by the number of elements:

Code: Select all

``````myvar := "1 2 3 4"
msgbox % mean(myvar)
return

mean(var){
for x,y in z := strsplit(var," ")
sum += y
return sum / z.length()
}``````
Chunjee
Posts: 524
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

### Re: Help with sum and dividing numbers?

I don't understand this German number format or whatever it is; but this seems to work:

Code: Select all

``````A := new biga() ; requires https://www.npmjs.com/package/biga.ahk

values := "14 374,00	21 487,00	22 460,00 21 562,00"
values := A.replace(values, "/\s/") ;remove whitespace
values := A.compact(A.split(values, ",00")) ; split into array and drop empties

msgbox, % A.mean(values)
; => 19970.750000
``````
for fun
Posts: 1643
Joined: 29 Mar 2015, 09:41
Contact:

### Re: Help with sum and dividing numbers?  Topic is solved

Code: Select all

``````nums := "14 374,00 21 487,00 22 460,00 21 562,00"
sum := i := 0
while RegExMatch(nums, "O)\d[\d ]*,\d+", m, m ? m.Pos + m.Len : 1) {
n := StrReplace(m, " ")
sum += StrReplace(n, ",", ".")
i++
}
MsgBox, % sum/i``````
BNOLI
Posts: 548
Joined: 23 Mar 2020, 03:55

### Re: Help with sum and dividing numbers?

Chunjee wrote:
20 May 2020, 15:20
I don't understand this German number format or whatever it is; but this seems to work:
JFTR, this isn't the standard German (currency) number format. Normaly it wouldn't contain a whitespace character. I'd guess in its place there was the thousands seperator (a dot, that has been replaced beforehand?).

Standard German floating "point" number/currency format: 21.321,98 €
Just using a comma instead of the floating point, that's it. The 'thousand separator'/the dot isn't mandatory (and not used in math), but only used for readability:
21.000.000 = 21million,
1.000.000
100.000 €
12.000
2.355 €
120 €

Code: Select all

``````; RegEx free
str:= "23.000,99 10.000,99 9.000,99 15.000,00"
index := StrSplit(StrReplace(StrReplace(str,".",""),",","."),A_Space)

for each, no in index
sum += no
MsgBox % "sum:`t" sum "`nsum/" index.Length() ":`t" sum / index.Length()
`````` Remember to use [code]CODE[/code]-tags for your multi-line scripts. Stay safe, stay inside, and remember washing your hands for 20 sec !
Krd
Posts: 52
Joined: 10 Mar 2020, 02:46

### Re: Help with sum and dividing numbers?

20 May 2020, 15:37

Code: Select all

``````nums := "14 374,00 21 487,00 22 460,00 21 562,00"
sum := i := 0
while RegExMatch(nums, "O)\d[\d ]*,\d+", m, m ? m.Pos + m.Len : 1) {
n := StrReplace(m, " ")
sum += StrReplace(n, ",", ".")
i++
}
MsgBox, % sum/i``````
Fantastic! This is what I wanted!

Code: Select all

``````^+r::
nums := clipboard
sum := i := 0
while RegExMatch(nums, "O)\d[\d ]*,\d+", m, m ? m.Pos + m.Len : 1) {
n := StrReplace(m, " ")
sum += StrReplace(n, ",", ".")
i++
}
Sendinput, % sum/i
return``````
Can you please tell me how to remove the dot and replace it with a comma?
Because I paste it to Exel after that and it does not recognize the numberformat. A comma would fix it.
Is it also possible to remove all other numbers other than the first two ones afer coma/dot?

Really appreciated and it worked on all variants I needed it for!
Posts: 1643
Joined: 29 Mar 2015, 09:41
Contact:

### Re: Help with sum and dividing numbers?

Krd wrote: how to remove the dot and replace it with a comma?
Sendinput, % StrReplace(sum/i, ".", ",")
just me
Posts: 7114
Joined: 02 Oct 2013, 08:51
Location: Germany

### Re: Help with sum and dividing numbers?

Krd wrote:Is it also possible to remove all other numbers other than the first two ones afer coma/dot?

Code: Select all

``SendInput, % StrReplace(Round(Sum / I, 2), ".", ",")``
Krd
Posts: 52
Joined: 10 Mar 2020, 02:46

### Re: Help with sum and dividing numbers?

just me wrote:
22 May 2020, 05:04
Krd wrote:Is it also possible to remove all other numbers other than the first two ones afer coma/dot?

Code: Select all

``SendInput, % StrReplace(Round(Sum / I, 2), ".", ",")``
Hehe, thanks for seen the last question ^^ Worked like a charm!