Page 1 of 1

sum numbers from clipboard

Posted: 24 Mar 2021, 00:25
by ivill
1000.00
2000.00
3000.00
4000.00

how do i supposed to sum such numbers from clipboard as shown above?
anyone help, Appreciate !

Re: sum numbers from clipboard  Topic is solved

Posted: 24 Mar 2021, 00:43
by Jim Dunn

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
}

Re: sum numbers from clipboard

Posted: 24 Mar 2021, 03:15
by ivill
FIrst of all, thanks for your help.

Re: sum numbers from clipboard

Posted: 24 Mar 2021, 03:16
by ivill
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!