How to make a 4-digit number by adding leading zeros

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
helpme
Posts: 12
Joined: 19 Apr 2014, 10:28

How to make a 4-digit number by adding leading zeros

07 Jun 2014, 08:00

I would like to add leading zeros to a number such that it always becomes a 4-digit number. For example, if the number is 12, then the converted number is 0012. If it is 122, the the converted number is 0122. If it is 1, the the converted number is 0001.

How can this be done in Autohotkey? Thank you.

I am using Autohotkey ver1.1.15
just me
Posts: 9458
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to make a 4-digit number by adding leading zeros

07 Jun 2014, 08:26

One option:

Code: Select all

#NoEnv
MyNumber := 1
MsgBox, 0, 4-digit Number, % SubStr("000" . MyNumber, -3)
ExitApp
Ronins
Posts: 85
Joined: 02 Oct 2013, 11:38

Re: How to make a 4-digit number by adding leading zeros

07 Jun 2014, 11:43

Another way

Code: Select all

Num := 12
Pack := "0000"
MsgBox, % (SubStr(Pack, 1, StrLen(Pack) - StrLen(Num)) . Num)
Try out CMD class
User avatar
joedf
Posts: 8958
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: How to make a 4-digit number by adding leading zeros

07 Jun 2014, 16:09

@just me ha ha pretty close... that's what I pretty much had :P
http://stackoverflow.com/a/24100686
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
tank
Posts: 3122
Joined: 28 Sep 2013, 22:15
Location: CarrolltonTX
Contact:

Re: How to make a 4-digit number by adding leading zeros

07 Jun 2014, 16:48

http://msdn.microsoft.com/en-us/library ... p/ms647550
or you can get this directly from the documentation
DllCall
DllCall
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter
Gangula
Posts: 31
Joined: 25 Oct 2017, 11:15

Re: How to make a 4-digit number by adding leading zeros

27 Oct 2017, 20:40

joedf wrote:@just me ha ha pretty close... that's what I pretty much had :P
http://stackoverflow.com/a/24100686
How do i add a padding code into this? I'd like to "SetFormat, float, 03.0". But I don't know how and where to add this line of code to.
User avatar
joedf
Posts: 8958
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: How to make a 4-digit number by adding leading zeros

27 Oct 2017, 21:14

You want padding for a float number?
I’m not sure what you mean here.
Do you have code to share?
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Gangula
Posts: 31
Joined: 25 Oct 2017, 11:15

Re: How to make a 4-digit number by adding leading zeros

07 Nov 2017, 01:01

https://autohotkey.com/board/topic/2530 ... ng-string/

` & w::
clip := Clipboard
if RegExMatch(clip, "s)(.*?)(\d+)(.*)", m)
Clipboard := m1 . m2+1 . m3
return

I found a code in one of the forums for pasting incrementing string(above code)

But if I have a string "000abc" it returns a value of "1abc". But my desired output is "001abc". How do I achieve that?
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: How to make a 4-digit number by adding leading zeros

07 Nov 2017, 05:50

Code: Select all

Clipboard := m1 . Format("{:03}", m2+1) . m3
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to make a 4-digit number by adding leading zeros

07 Nov 2017, 05:51

Here's a way:

Code: Select all

q::
vText := "1abc"
MsgBox, % Format("{:06}", vText)
return
For more Format function examples see FORMAT FUNCTION EXAMPLES here:
jeeswg's characters tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=26486
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Gangula
Posts: 31
Joined: 25 Oct 2017, 11:15

Re: How to make a 4-digit number by adding leading zeros

07 Nov 2017, 09:00

Worked like a charm. Thanks a lot.
Gangula
Posts: 31
Joined: 25 Oct 2017, 11:15

Re: How to make a 4-digit number by adding leading zeros

08 Nov 2017, 03:11

Odlanir wrote:

Code: Select all

Clipboard := m1 . Format("{:03}", m2+1) . m3
Just one more thing. Do you know how to let the code detect the format and then apply it.

In the code you specified, it's fixed to 3 leading 0's. I'm wondering if it's possible to write a script that detects the number of leading zero's in the parent string and apply that as the format.
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: How to make a 4-digit number by adding leading zeros

08 Nov 2017, 04:59

Sure:

Code: Select all

clip := Clipboard
if RegExMatch(clip, "s)(.*?)(\d+)(.*)", m) {
   lm2 := strlen(m2)
   Clipboard := m1 . Format("{:0" lm2 "}", m2+1) . m3
}
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Gangula
Posts: 31
Joined: 25 Oct 2017, 11:15

Re: How to make a 4-digit number by adding leading zeros

16 Nov 2017, 02:35

The more I use this code, the more features I find to add to this script.
Can someone help me with adding few other lines of code to this?

This code works like a charm, but I'd like to add "F2" and "CTRL+A" into this script.
So what I want is to, click on a file and enter the shortcut key, so that it'll rename it without me having to manually press "F2" and "CTRL+A".

Code: Select all

` & w::
    clip := Clipboard
if RegExMatch(clip, "s)(.*?)(\d+)(.*)", m) {
   lm2 := strlen(m2)
   Clipboard := m1 . Format("{:0" lm2 "}", m2+1) . m3
}
    sendinput ^v
return
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: How to make a 4-digit number by adding leading zeros

16 Nov 2017, 04:11

Select a file ( only one ) in explorer window and press window-key+3

Code: Select all

#SingleInstance, force
#Persistent
#NoEnv

#3::
OldName := Explorer_GetSelection()
if (RegExMatch(OldName,"`n") > 0 ) {
   MsgBox % "You must select only one file."
   ExitApp
}
if RegExMatch(OldName, "Os)(.*?)(\d+)(.*)", m) {
   lm2 := strlen(m[2])
   NewName := m[1] . Format("{:0" lm2 "}", m[2]+1) . m[3]
   FileMove, % OldName, % NewName
} else {
   MsgBox % "The selected file does not contains any digit."
}   
return


Explorer_GetSelection(hwnd="") {
	hwnd := hwnd ? hwnd : WinExist("A")
	WinGetClass class, ahk_id %hwnd%
	if (class="CabinetWClass" or class="ExploreWClass" or class="Progman" or class="#32770")
		for window in ComObjCreate("Shell.Application").Windows
			if (window.hwnd==hwnd)
    sel := window.Document.SelectedItems
	for item in sel
	ToReturn .= item.path "`n"
	return Trim(ToReturn,"`n")
}


esc::
   ExitApp
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Gangula
Posts: 31
Joined: 25 Oct 2017, 11:15

Re: How to make a 4-digit number by adding leading zeros

16 Nov 2017, 08:06

The script works perfectly. But I'd like it to work with normal string, not just on files. I've given renaming files as an example.

So, here are the steps I'd like the script to folllow:
  1. I'll copy a text from anywhere
  2. then I'll click somewhere else(notepad/files or any other software)
  3. then I'll press the hotkey which executes F2 > CTRL+A > then pastes the incremented text
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: How to make a 4-digit number by adding leading zeros

16 Nov 2017, 14:22

Is this what you want?

Code: Select all

#SingleInstance, force
#Persistent
#NoEnv

#3::
if RegExMatch(Clipboard, "O)(.*?)(\d+)(.*)", m) {
   lm2 := strlen(m[2])
   Clipboard := m[1] . Format("{:0" lm2 "}", m[2]+1) . m[3]
   send  {F2}
   Send ^a
   send ^v
}
return
esc::
   ExitApp
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Gangula
Posts: 31
Joined: 25 Oct 2017, 11:15

Re: How to make a 4-digit number by adding leading zeros

17 Nov 2017, 08:03

Wow, yeah. I didn't realize it was this simple. Thank you
Gangula
Posts: 31
Joined: 25 Oct 2017, 11:15

Re: How to make a 4-digit number by adding leading zeros

04 Dec 2017, 02:09

Odlanir wrote:Is this what you want?

Code: Select all

#SingleInstance, force
#Persistent
#NoEnv

#3::
if RegExMatch(Clipboard, "O)(.*?)(\d+)(.*)", m) {
   lm2 := strlen(m[2])
   Clipboard := m[1] . Format("{:0" lm2 "}", m[2]+1) . m[3]
   send  {F2}
   Send ^a
   send ^v
}
return
esc::
   ExitApp
Hey Odlanir, I've created a separate thread for this for another query. Can you please look into that?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, Google [Bot] and 386 guests