Rob van der Woude's Scripting Pages

Add/Remove Leading Zeroes

There are several techniques to add or remove leading zeroes.
I will list some of these here, all of them intended for unsigned decimal numbers.

 

To recognize line wrapping, command lines are interlaced with blank lines.
So in the following tables, you should treat command lines not separated by blank lines as a single command line.

 

Remove Leading Zeroes
# Command
Pros and Cons
1 SET /A Var = 100%Var% %% 100
  • Fail-safe for numbers up to and including 99, with or without leading zero
  • Can be scaled up easily to more digits
  • Since math is used (SET /A), this technique can only be used for numbers of up to 5 digits (3)
2 SET Var=10%Var%

IF %Var% GTR 1000 SET /A Var=%Var%-1000

IF %Var% GTR  100 SET /A Var=%Var%-100
  • Fail-safe for numbers up to and including 99, with or without leading zero
  • Can be scaled up easily to more digits
  • Way too much code for just 2 digits
  • Since math is used (IF ... GTR ... and SET /A), this technique can only be used for numbers of up to 5 digits (3)
3 IF "%Var:~0,1%"=="0" SET Var=%Var:~1%
  • Fail-safe for numbers up to and including 99, with or without leading zero
  • Can be scaled up easily: just repeat the command once for every added digit
  • Can only be used for fixed number of digits
  • Returns an empty string for 0 or a string of zeroes (00, 000, 0000...)
4 :Loop

IF "%Var:~0,1%"=="0" (

  SET Var=%Var:~1%

  GOTO Loop

)
  • Fail-safe for any number, with or without leading zero
  • A lot of code; use only for larger numbers or in case the number of digits is unpredictable
  • Returns an empty string for a string of zeroes
5 FOR /F "tokens=* delims=0" %%A IN ("%Var%") DO SET Var=%%A
  • Fail-safe for any number, with or without leading zero
  • Returns an empty string for a string of zeroes
6 FOR /F "tokens=* delims=0" %%A IN ("%Var%") DO SET /A Var=%%A+0
  • Fail-safe for any number up to 9 digits (2), with or without leading zero
  • Returns a single zero for a string of zeroes
  • Since math is used (SET /A), this technique can only be used for numbers of up to 9 digits (2)
7 FOR /F "tokens=* delims=0" %%A IN ("%Var%") DO SET Var=%%A

SET /A Var += 0 2>NUL
  • Fail-safe for any number, with or without leading zero
  • Returns a single zero for a string of zeroes
  • Though math is used in the second line, it is only required for 0, so if it fails on 10-digit numbers that won't affect the resulting value
  • Though a math failure on 10-digit numbers may not affect the resulting value, it does raise an error, which could affect the batch code's ErrorLevel
8 FOR /F "tokens=* delims=0" %%A IN ("%Var%") DO SET Var=%%A

IF "%Var%"=="" SET Var=0
  • Fail-safe for any number, with or without leading zero
  • Returns a single zero for a string of zeroes
  • Preferred!

 

Of all techniques to remove leading zeroes presented here, #8 is the preferred one, as it will work for any number, without any restriction (5).

 

Note: To remove leading zeroes from times' minutes and seconds, this clever technique by Andrew Dent may do the trick:
SET TimeWithoutLeadingZeroes=%Time::0=:%
This would fail only in the unlikely case that %Time% does not use leading zeroes in minutes and seconds (e,g. 2:0:12 would be set to 2::12).
To remove leading zeroes from the hours will require additional code.

 

Add Leading Zeroes
# Command
Pros and Cons
1 IF %Var% LSS 10 SET Var=0%Var%
  • Works only for numbers 0..9 (without leading zero), 00..07 (with leading zero), and 10..99
  • Fails on 08 and 09 if %Var% does have a leading zero, because batch file math interprets numbers with leading zeroes as octal
  • Since math is used (IF ... LSS ...), this technique can only be used for numbers of up to 9 digits (2)
  • Not recommended!
2 IF 1%Var% LSS 100 SET Var=0%Var%
  • Fail-safe for numbers up to and including 99, with or without leading zero
  • Since math is used (IF ... LSS ...), this technique can only be used for numbers of up to 9 digits (2)
3 IF 1%Var% LSS 100 SET Var=0%Var%

IF 1%Var% LSS 1000 SET Var=0%Var%
  • Fail-safe for numbers up to and including 999, with or without leading zero
  • Can be scaled up easily to up to 9 digits (2)
  • Since math is used (IF ... LSS ...), this technique can only be used for numbers of up to 9 digits (2)
4 SET Var=00%Var%

SET Var=%Var:~-2%
  • Fail-safe for numbers up to and including 99, with or without leading zero, and even for an empty string
  • Can be scaled up easily to any number of digits (5)
  • Truncates larger numbers, e.g. if %Var% equals 123 then SET Var=%Var:~-2% returns 23
  • Preferred!

 

Of all techniques to add leading zeroes presented here, #4 is the preferred one, as it will work for any number, without any restriction in length (5).
However, higher numbers will be truncated, e.g. if %Var% equals 123 this technique returns 23.
See note 4 for a safer alternative using PowerShell or third party software.

 

Notes:
(1) Modulo division method (first command in the first table) by Paul Ruggieri.
(2) Math based techniques can be used for numbers up to 9 digits for Windows XP and later, or up to 4 digits for Windows 2000.
(3) Math based techniques with "prefixes" can be used for numbers up to 5 digits for Windows XP and later, and only 1 or 2 digits for Windows 2000.
(4) My own ToString.exe can also be used to add leading zeroes, e.g. ToString.exe "{0:D3}" 5 will return 005
It will not truncate higher numbers, e.g. ToString.exe "{0:D2}" 123 will return 123
The same result with native code only can be achieved using powershell -c "'{0:D2}' -f %Var%" (or pwsh -c "'{0:D2}' -f %Var%")
(5) Actually, there is one restiction: the batch language's line length limit.
However, unless you intend to use close to 900 digits, this won't be a problem.
(6) All of these commands are intended for decimal digits only, no signs allowed.

page last modified: 2023-08-01; loaded in 0.0023 seconds