Remove leading zeros in batch file

后端 未结 4 1634
逝去的感伤
逝去的感伤 2020-12-05 23:57

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

4条回答
  •  心在旅途
    2020-12-06 00:46

    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"
    

提交回复
热议问题