Format List of Numbers

Get help with using AutoHotkey and its commands and hotkeys
diekfj13
Posts: 2
Joined: 16 Sep 2020, 09:59

Format List of Numbers

Post by diekfj13 » 16 Sep 2020, 10:14

Hi -

I created a hotkey script that sends a number, followed by the tab key twice, then the next number, repeating until there are no more numbers.

In the example below CTRL+= will send 1 tab tab 2 tab tab 3, so the result when pasting in excel, will have "1" in cell A1 "2" in cell C1 and "3" in cell E1

My question is, how can I have hotkey format all of the numbers, to a specific decimal format. My end result needs to always be in the format 0.000 whether I paste it in excel, notepad word etc.
So end result should be 1.000 tab tab 2.000 tab tab 3.000

I have tried using Format, but it will not format all the numbers listed so not sure if i need to convert to an array?
Send % Format("{:.3f}", 1 2 3)
Results in 123.000

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
^=::

Send,	1		2		3

Return

^r:: Reload ;CTRL+S saves the script CTRL+r reloads

Return

^e:: Edit ;CTRL+e opens script to edit
[Mod edit: [code][/code] tags added.]

User avatar
Xtra
Posts: 1919
Joined: 02 Oct 2015, 12:15

Re: Format List of Numbers

Post by Xtra » 16 Sep 2020, 10:31

Code: Select all

numbers := [1,2,3]
for each, num in numbers
    Send % Format((A_Index=1 ? "" : "`t`t")"{:.3f}", num)

User avatar
SKAN
Posts: 817
Joined: 29 Sep 2013, 16:58

Re: Format List of Numbers

Post by SKAN » 17 Sep 2020, 14:02

diekfj13 wrote: I have tried using Format, but it will not format all the numbers listed so not sure if i need to convert to an array?
Send % Format("{:.3f}", 1 2 3)
Results in 123.000
 

Code: Select all

#NoEnv
#SingleInstance, Force

^=::SendInput % MyFormat(1,2,3)

MyFormat(P*) {
Return Format(RTrim(StrReplace(Format("{:0" . P.Count() . "}",0),0,"{:.3f}`t`t"),"`t"),P*)
}

teadrinker
Posts: 1865
Joined: 29 Mar 2015, 09:41
Contact:

Re: Format List of Numbers

Post by teadrinker » 17 Sep 2020, 14:35

@SKAN, interesting solution! :)

Code: Select all

MsgBox, % "|" MyFormat(1,2,3,4) "|"

MyFormat(P*) {
   Return Format("{:.3f}", P.RemoveAt(1)) . (P.Count() ? "`t`t" . MyFormat(P*) : "")
}

User avatar
SKAN
Posts: 817
Joined: 29 Sep 2013, 16:58

Re: Format List of Numbers

Post by SKAN » 17 Sep 2020, 14:54

Hi @teadrinker

Neat!. :thumbup:

Recursive calling is too slow.
My original attempt was to beat loop based Join() with format.. No joy. Join() always wins.

diekfj13
Posts: 2
Joined: 16 Sep 2020, 09:59

Re: Format List of Numbers

Post by diekfj13 » 19 Sep 2020, 17:15

Thank you this worked!

Post Reply

Return to “Ask For Help”