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.

 

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
  • 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.

 

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) All of these commands are intended for digits only, no signs allowed.

page last uploaded: 2015-09-03, 08:32