sum numbers from clipboard Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

sum numbers from clipboard

24 Mar 2021, 00:25

1000.00
2000.00
3000.00
4000.00

how do i supposed to sum such numbers from clipboard as shown above?
anyone help, Appreciate !
User avatar
Jim Dunn
Posts: 478
Joined: 01 Sep 2020, 20:21
Location: NSW

Re: sum numbers from clipboard  Topic is solved

24 Mar 2021, 00:43

Code: Select all

ClipBoard = 
(
1000.00
2000.00
3000.00
4000.00
)
; ^^^ this is to simulate what is on your clipboard for this test

MsgBox, % fnSumNumericLines(ClipBoard)

fnSumNumericLines(InputVar) {
	Loop, Parse, InputVar, `r`n 
		Result += A_LoopField
	Result := Format("{:.2f}", Result)  ; if you want it formatted
	Return Result
}
See https://www.autohotkey.com/docs/commands/Format.htm for other ways you could format Result

I created it as a function to make it more portable/reusable throughout your code. You could even make some of the "format" into a parameter you supply, something like this example, specifying decimal places (but defaulting to 2):

Code: Select all

ClipBoard = 
(
1000.00
2000.00
3000.00
4000.00
)
; ^^^ this is to simulate what is on your clipboard for this test

MsgBox, % fnSumNumericLines(ClipBoard, 2)
MsgBox, % fnSumNumericLines(ClipBoard, 0)
MsgBox, % fnSumNumericLines(ClipBoard, 4)
MsgBox, % fnSumNumericLines(ClipBoard)		; no second parameter - uses function "default" of 2

fnSumNumericLines(InputVar, DecimalPlaces:=2) {
	Loop, Parse, InputVar, `r`n 
		Result += A_LoopField
	Result := Format("{:." DecimalPlaces "f}", Result)
	Return Result
}
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: sum numbers from clipboard

24 Mar 2021, 03:15

FIrst of all, thanks for your help.
Last edited by ivill on 24 Mar 2021, 03:16, edited 1 time in total.
User avatar
ivill
Posts: 124
Joined: 13 May 2016, 02:23

Re: sum numbers from clipboard

24 Mar 2021, 03:16

Jim Dunn wrote:
24 Mar 2021, 00:43

Code: Select all

ClipBoard = 
(
1000.00
2000.00
3000.00
4000.00
)
; ^^^ this is to simulate what is on your clipboard for this test

MsgBox, % fnSumNumericLines(ClipBoard)

fnSumNumericLines(InputVar) {
	Loop, Parse, InputVar, `r`n 
		Result += A_LoopField
	Result := Format("{:.2f}", Result)  ; if you want it formatted
	Return Result
}
See https://www.autohotkey.com/docs/commands/Format.htm for other ways you could format Result

I created it as a function to make it more portable/reusable throughout your code. You could even make some of the "format" into a parameter you supply, something like this example, specifying decimal places (but defaulting to 2):

Code: Select all

ClipBoard = 
(
1000.00
2000.00
3000.00
4000.00
)
; ^^^ this is to simulate what is on your clipboard for this test

MsgBox, % fnSumNumericLines(ClipBoard, 2)
MsgBox, % fnSumNumericLines(ClipBoard, 0)
MsgBox, % fnSumNumericLines(ClipBoard, 4)
MsgBox, % fnSumNumericLines(ClipBoard)		; no second parameter - uses function "default" of 2

fnSumNumericLines(InputVar, DecimalPlaces:=2) {
	Loop, Parse, InputVar, `r`n 
		Result += A_LoopField
	Result := Format("{:." DecimalPlaces "f}", Result)
	Return Result
}
Appreciate , problem solved!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, Leonardo_Portela and 128 guests