19

In my application, I get a number having leading zeros. I am trying to trim the leading zeros and get the actual number. I tried using /a switch which considers right side of the assignment as an arithmetic expression. So I tried:

SET /a N = 00027

The above gave me the output of 23 which is the decimal equivalent of octal number 27. Then I found this solution online.

SET N = 00027
SET /a N = 1%N%-(11%N%-1%N%)/10

This seems working and is giving the output 27. Is there much easier way to trim the leading zeros in a batch file?

  • Not that I can find. :-) The problem is that, as you can see, the leading zero without a following x indicates that you're using an octal number (0x0027 indicates a hex number). The only way to remove it is to concatenate a non-zero value on the left to make it appear to be a decimal value and then remove the amount added. (You can see the number formats supported by using a known invalid value in the operation, like set /a N=d/1, which produces 'Invalid number. Numeric constants are either decimal (17), hexadecimal (0x11), or octal (021).". – Ken White Feb 7 '13 at 23:18
  • 1
    You found a one-liner solution that does what you want. What else do you need? :) – mrt Feb 8 '13 at 6:48
  • See also: robvanderwoude.com/battech_leadingzero.php – Dave Jarvis Oct 26 '17 at 22:27
18

The method you found is very good. It supports numbers up to 99,999,999 and is very fast.

There is a simpler way to use SET /A that works with numbers up to 9999. It uses the modulus operation. The method cannot be extended to larger numbers.

 set n=0027
 set /a n=10000%n% %% 10000

The FOR /F method that Dale posted works with "any" size number (up to 8191 digits). However, it needs just a bit more work to handle zero values.

set n=000000000000000000000000000000000000000000000000000027
for /f "tokens=* delims=0" %%N in ("%n%") do set "n=%%N"
if not defined n set "n=0"
  • 1
    Looks like the for /f method doesn't work for only zeros, since the for/f sets n to nothing. I would rather check for zero like this: if not defined n set /a n = 0 – timlg07 Jun 6 at 14:53
  • 1
    @timlg07 - OMG! I don't know what I was thinking when I wrote that. Actually I do - I was thinking that the loop would not iterate because there was no value, but that is only true if "%n%" is empty or contains whitespace. But I never bothered to test (boo to me). I am shocked you are the first person to point out the error. I've fixed the post. Thanks – dbenham Jun 6 at 15:26
4

You can use FOR /F to remove leading zeros.

C:\>SET n=00030
echo off
for /f "tokens=* delims=0" %a in ("%n%") DO echo %a
30

As you can see, the delims is set to 0. This will makes 0 as a delimiter. At the same time with tokens of * this will ensure that the leading 0's will be removed while the rest of the line will be processed (including trailing 0's).

You may refer to this link for more information about removal of leading 0's.

P.S. Do remember to use %%a instead of %a when you are running on batch file in FOR /F.

  • 1
    +1, but you need a bit more work to handle 0 values. – dbenham Feb 8 '13 at 12:00
4

The exit command is pretty good at clearing leading zeros:

>set n=0000890

>cmd /c exit /b %n%

>echo %errorlevel%
890

With this you can use number up to 32 bit integer limit and the piece of code is really small, despite additional call of cmd.exe could harm the performance.

  • 1
    +1, Good method, but you need to be aware of the limitations. ERRORLEVEL is a signed 32 bit integer with a value between -2147483648 and 2147483647. An EXIT value that exceeds those limits will be transformed to one within the limits. For example, EXIT 4000000000 is reported as ERRORLEVEL -294967296. – dbenham Jan 7 '16 at 12:58
-1

If it's only a matter of removing all zeros, you can do it like this:

> SET n=00000002304650620000.23094400
> SET n=%n:0=%
2346562.23944
  • 1
    Err, at no point did the question not specify that it was leading zeros to be removed so this answer is about as useful to the question as if I had posted a lasagna recipe :-) – paxdiablo Feb 5 '18 at 1:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.