Help with sum and dividing numbers? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Help with sum and dividing numbers?

20 May 2020, 10:40

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
User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Help with sum and dividing numbers?

20 May 2020, 11:42

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()
	}
14.3 & 1.3.7
User avatar
Chunjee
Posts: 1422
Joined: 18 Apr 2014, 19:05
Contact:

Re: Help with sum and dividing numbers?

20 May 2020, 15:20

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
teadrinker
Posts: 4330
Joined: 29 Mar 2015, 09:41
Contact:

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

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[0], " ")
   sum += StrReplace(n, ",", ".")
   i++
}
MsgBox, % sum/i
BNOLI
Posts: 548
Joined: 23 Mar 2020, 03:55

Re: Help with sum and dividing numbers?

21 May 2020, 00:17

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: 405
Joined: 10 Mar 2020, 02:46

Re: Help with sum and dividing numbers?

22 May 2020, 04:01

teadrinker wrote:
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[0], " ")
   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[0], " ")
   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!
teadrinker
Posts: 4330
Joined: 29 Mar 2015, 09:41
Contact:

Re: Help with sum and dividing numbers?

22 May 2020, 04:22

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

Re: Help with sum and dividing numbers?

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), ".", ",")
Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: Help with sum and dividing numbers?

22 May 2020, 05:43

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!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, jaka1, LuckyJoe, Rohwedder and 327 guests