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
Help with sum and dividing numbers? Topic is solved
- flyingDman
- Posts: 2817
- 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()
}
14.3 & 1.3.7
Re: Help with sum and dividing numbers?
I don't understand this German number format or whatever it is; but this seems to work:
for fun
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
-
- Posts: 4330
- 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[0], " ")
sum += StrReplace(n, ",", ".")
i++
}
MsgBox, % sum/i
Re: Help with sum and dividing numbers?
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 !
Re: Help with sum and dividing numbers?
Fantastic! This is what I wanted!teadrinker wrote: ↑20 May 2020, 15:37Code: 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
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
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: 4330
- Joined: 29 Mar 2015, 09:41
- Contact:
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), ".", ",")
Re: Help with sum and dividing numbers?
Hehe, thanks for seen the last question ^^ Worked like a charm!just me wrote: ↑22 May 2020, 05:04Krd 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), ".", ",")