1000.00
2000.00
3000.00
4000.00
how do i supposed to sum such numbers from clipboard as shown above?
anyone help, Appreciate !
sum numbers from clipboard Topic is solved
Re: sum numbers from clipboard Topic is solved
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
}
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
FIrst of all, thanks for your help.
Last edited by ivill on 24 Mar 2021, 03:16, edited 1 time in total.
Re: sum numbers from clipboard
Appreciate , problem solved!Jim Dunn wrote: ↑24 Mar 2021, 00:43See https://www.autohotkey.com/docs/commands/Format.htm for other ways you could format ResultCode: 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 }
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 }