Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I remove the leading zeros a non-negative integer?

917 views
Skip to first unread message

Timo Salmi

unread,
Sep 27, 2005, 2:20:27 PM9/27/05
to
DRAFT

119) How can I remove the leading zeros a non-negative integer?

@echo off & setlocal enableextensions
set s_= 007
echo %s_%
echo %s_%|findstr [123456789]>nul
if %errorlevel% EQU 0 (
for /f "tokens=* delims=0 " %%a in ('echo %s_%') do (
set s_=%%a)
) else (set s_=0)
echo %s_%
endlocal & goto :EOF

The output will be
C:\_D\TEST>cmdfaq
007
7

Or, using a subroutine
@echo off & setlocal enableextensions
set s_=000
echo %s_%
call :TrimZeros %s_% s_
echo %s_%
endlocal & goto :EOF
::
:: =============================================
:TrimZeros Dymmy1 Return_
setlocal enableextensions
echo %1|findstr [123456789]>nul
if %errorlevel% EQU 0 (
for /f "tokens=* delims=0 " %%a in ('echo %1') do (
set Return_=%%a)
) else (set Return_=0)
endlocal & set "%2=%Return_%" & goto :EOF

The output will be
C:\_D\BAS>cmdfaq
000
0

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip

William Allen

unread,
Sep 27, 2005, 4:09:31 PM9/27/05
to
"Timo Salmi" wrote in message

> DRAFT
>
> 119) How can I remove the leading zeros a non-negative integer?
...snip

What's wrong with the obvious (it's immaterial in this case that
SET /a will treat the argument as Octal):

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF
SET N=007
SET /a N+=0
ECHO. N=%N%

====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

============Screen capture Windows 2000 simulated in Win95
C:\WORK>demo
N=7

C:\WORK>
============End screen capture

--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
From email address not checked. Contact us at http://www.allenware.com/


Ted Davis

unread,
Sep 27, 2005, 4:52:29 PM9/27/05
to
On Tue, 27 Sep 2005 21:09:31 +0100, "William Allen" <_w...@email.com>
wrote:

>"Timo Salmi" wrote in message
>> DRAFT
>>
>> 119) How can I remove the leading zeros a non-negative integer?
>...snip
>
>What's wrong with the obvious (it's immaterial in this case that
>SET /a will treat the argument as Octal):
>
>Lines that don't begin with two spaces have wrapped accidentally
>====Begin cut-and-paste (omit this line)
> @ECHO OFF
> SET N=007
> SET /a N+=0
> ECHO. N=%N%
>
>====End cut-and-paste (omit this line)

Screen dump
----------
d:\MyFiles>SET N=009

d:\MyFiles>SET /a N+=0

d:\MyFiles>ECHO. N=0
N=0

d:\MyFiles>
----------
--
T.E.D. (tda...@gearbox.maem.umr.edu)
SPAM filter: Messages to this address *must* contain "T.E.D."
somewhere in the body or they will be automatically rejected.

William Allen

unread,
Sep 27, 2005, 6:21:57 PM9/27/05
to
"Ted Davis" wrote in message

> "William Allen" wrote:
>
> >"Timo Salmi" wrote in message
> >> DRAFT
> >>
> >> 119) How can I remove the leading zeros a non-negative integer?
> >...snip
> >
> >What's wrong with the obvious (it's immaterial in this case that
> >SET /a will treat the argument as Octal):
> >
> >Lines that don't begin with two spaces have wrapped accidentally
> >====Begin cut-and-paste (omit this line)
> > @ECHO OFF
> > SET N=007
> > SET /a N+=0
> > ECHO. N=%N%
> >
> >====End cut-and-paste (omit this line)
>
> Screen dump
> ----------
> d:\MyFiles>SET N=009
>
> d:\MyFiles>SET /a N+=0
>
> d:\MyFiles>ECHO. N=0
> N=0
>
> d:\MyFiles>

<G> Thanks, Ted. Good point, so I guess it has to be the
slightly-less obvious:

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF

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

====End cut-and-paste (omit this line)

Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

============Screen capture Windows 2000 simulated in Win95
C:\WORK>demo

N=9

Timo Salmi

unread,
Sep 27, 2005, 11:50:39 PM9/27/05
to
William Allen <_w...@email.com> wrote:
> "Timo Salmi" wrote in message
> > 119) How can I remove the leading zeros a non-negative integer?

> What's wrong with the obvious (it's immaterial in this case that


> SET /a will treat the argument as Octal):

Just one trivial example


@echo off & setlocal enableextensions

set s_=008
set /a s_=%s_%+2


endlocal & goto :EOF
The output will be
C:\_D\TEST>cmdfaq

Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).

William Allen

unread,
Sep 28, 2005, 2:27:59 AM9/28/05
to
"Timo Salmi" wrote in message
> William Allen wrote:
> > "Timo Salmi" wrote in message
> > > 119) How can I remove the leading zeros a non-negative integer?
>
> > What's wrong with the obvious (it's immaterial in this case that
> > SET /a will treat the argument as Octal):
>
> Just one trivial example
> @echo off & setlocal enableextensions
> set s_=008
> set /a s_=%s_%+2
> endlocal & goto :EOF
> The output will be
> C:\_D\TEST>cmdfaq
> Invalid number. Numeric constants are either decimal (17),
> hexadecimal (0x11), or octal (021).

Already dealt with in my earlier reply to Ted with:

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF

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

ECHO N=%N%

====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

--

Timo Salmi

unread,
Sep 28, 2005, 4:06:52 AM9/28/05
to
In article <433a3813$0$49801$ed2e...@ptn-nntp-reader04.plus.net>,

William Allen <_w...@email.com> wrote:
> "Timo Salmi" wrote in message
> > William Allen wrote:
> > > "Timo Salmi" wrote in message
> > > > 119) How can I remove the leading zeros a non-negative integer?
> > > What's wrong with the obvious (it's immaterial in this case that
> > > SET /a will treat the argument as Octal):

William, I am sligthly baffled why a FAQ about removing leading
zeros (or some similar lead) should have a special justification (if
you pardon the pun). It always is good, though, to have alternative
methods. Thank you. I will be pleased to add your alternative.

Netvethless, let's take a look at


> @ECHO OFF
> SET N=009
> SET /a N=1%N%-(11%N%-1%N%)/10
> ECHO N=%N%

Take another number:


@echo off & setlocal enableextensions

goto _2
SET N=009111111


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

endlocal & goto :EOF

C:\_D\TEST>cmdfaq
Invalid number. Numbers are limited to 32-bits of precision.
N=009111111

which obviously is not the conversion result we want.

However:


@echo off & setlocal enableextensions

set s_=009111111


echo %s_%
echo %s_%|findstr [123456789]>nul
if %errorlevel% EQU 0 (
for /f "tokens=* delims=0 " %%a in ('echo %s_%') do (
set s_=%%a)
) else (set s_=0)
echo %s_%
endlocal & goto :EOF

C:\_D\TEST>cmdfaq
009111111
9111111

Timo Salmi

unread,
Sep 28, 2005, 4:00:44 AM9/28/05
to
William Allen <_w...@email.com> wrote:
> "Timo Salmi" wrote
> > William Allen wrote:
> > > "Timo Salmi" wrote
> > > > 119) How can I remove the leading zeros a non-negative integer?
> > > What's wrong with the obvious (it's immaterial in this case that
> > > SET /a will treat the argument as Octal):

William, I am sligthly baffled why a FAQ about removing leading


zeros (or some similar lead) should have a special justification (if
you pardon the pun). It always is good, though, to have alternative
methods. Thank you. I will be pleased to add your alternative.

Nevertheless, let's take a look at


> @ECHO OFF
> SET N=009
> SET /a N=1%N%-(11%N%-1%N%)/10
> ECHO N=%N%

Take another number:
@ECHO OFF
SET N=009111111


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

C:\_D\TEST>cmdfaq


Invalid number. Numbers are limited to 32-bits of precision.
N=009111111

which obviously is not the result we want.

However:


@echo off & setlocal enableextensions

set s_=009111111


echo %s_%
echo %s_%|findstr [123456789]>nul
if %errorlevel% EQU 0 (
for /f "tokens=* delims=0 " %%a in ('echo %s_%') do (
set s_=%%a)
) else (set s_=0)
echo %s_%
endlocal & goto :EOF

C:\_D\TEST>cmdfaq
009111111
9111111

All the best, Timo

foxidrive

unread,
Sep 28, 2005, 4:41:06 AM9/28/05
to
On Wed, 28 Sep 2005 08:00:44 +0000 (UTC), Timo Salmi wrote:

> However:
> @echo off & setlocal enableextensions
> set s_=009111111
> echo %s_%
> echo %s_%|findstr [123456789]>nul
> if %errorlevel% EQU 0 (
> for /f "tokens=* delims=0 " %%a in ('echo %s_%') do (
> set s_=%%a)
> ) else (set s_=0)
> echo %s_%
> endlocal & goto :EOF
>
> C:\_D\TEST>cmdfaq
> 009111111
> 9111111

Alternately:

@echo off & setlocal enableextensions
set s_=009111111
echo %s_%

:loop
set s_=%s_:*0=%
if "%s_:~0,1%"=="0" goto loop

Timo Salmi

unread,
Sep 28, 2005, 5:16:25 AM9/28/05
to

Yes, but if
set s_=000
then
C:\_D\TEST>cmdfaq
000
ECHO is off.

foxidrive

unread,
Sep 28, 2005, 5:22:45 AM9/28/05
to
On Wed, 28 Sep 2005 09:16:25 +0000 (UTC), Timo Salmi wrote:

> foxidrive <mi...@melbpc.org.au.gotcha.invalid> wrote:
>> On Wed, 28 Sep 2005 08:00:44 +0000 (UTC), Timo Salmi wrote:
>> Alternately:
>> @echo off & setlocal enableextensions
>> set s_=009111111
>> echo %s_%
>> :loop
>> set s_=%s_:*0=%
>> if "%s_:~0,1%"=="0" goto loop
>> echo %s_%
>> endlocal & goto :EOF
>
> Yes, but if
> set s_=000
> then
> C:\_D\TEST>cmdfaq
> 000
> ECHO is off.
>
> All the best, Timo

Now that's being picky - it removed all the leading zeros! <grin>

So add one line

if "%s_%"=="" set s_=0

William Allen

unread,
Sep 28, 2005, 5:33:29 AM9/28/05
to
"William Allen" wrote in message
...snip

> Already dealt with in my earlier reply to Ted with:
>
> Lines that don't begin with two spaces have wrapped accidentally
> ====Begin cut-and-paste (omit this line)
> @ECHO OFF
> SET N=009
> SET /a N=1%N%-(11%N%-1%N%)/10
> ECHO N=%N%
>
> ====End cut-and-paste (omit this line)
> Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
> Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

Or I suppose, the simpler:

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF
SET N=009

SET /a N=1%N%-2%N%+1%N%
ECHO. N=%N%

====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

============Screen capture Windows 2000 simulated in Win95
C:\WORK>demo
N=9

C:\WORK>
============End screen capture

and assorted variations on similar algebraic themes.

Message has been deleted

billious

unread,
Sep 28, 2005, 7:42:43 AM9/28/05
to

"Timo Salmi" <t...@uwasa.fi> wrote in message
news:dhc2hb$8t0$1...@haavi.uwasa.fi...


:: s_=non-negative integer; 009, 000, 0987, whatever...
set s_=009

:loop
if not %s_%==0 if %s_:~0,1%==0 set s_=%s_:~1%&goto loop

set s_


HTH

...Bill


Dr John Stockton

unread,
Sep 29, 2005, 9:54:40 AM9/29/05
to
JRS: In article <dhc2hb$8t0$1...@haavi.uwasa.fi>, dated Tue, 27 Sep 2005
18:20:27, seen in news:alt.msdos.batch.nt, Timo Salmi <t...@uwasa.fi>
posted :

>
>119) How can I remove the leading zeros a non-negative integer?
^ from

ISTM that +5 and --7 might be non-negative integers; perhaps "unsigned
integer" might be better?

ISTM that, at least by analogy with other computer languages, an integer
should be a binary construct, and does not have optional leading zeroes.
A number with leading zeroes must be a string.

ISTM that "the leading zeroes" implies that there is at least one, "any
leading zeroes" implies that there certainly may be none, and "leading
zeroes" leaves the point open.

119) How can I remove any leading zeros from an unsigned integer string?

The following seems to do it, and I suppose SED could be used likewise :

echo 00500 | mtr -o -x+ - ^(0*)(\d+) = \2

echo 00500 | mtr -o -x+ - ^(0*)(\d+)(.*) = \2

The second removes trailing material too.

However, those methods expect the leading character to be a digit.

echo 00500 | mtr -o -x+ - ^(\D*)(0*)(\d+)(.*) = \3

should extract the first number from a string - well, except for such as
ONE..2X where it gets the second number. There needs to be a digit
present, or the output line is lost; one needs to consider whether a
trailing CRLF is important.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
I find MiniTrue useful for viewing/searching/altering files, at a DOS prompt;
free, DOS/Win/UNIX, <URL:http://www.idiotsdelight.net/minitrue/>

Timo Salmi

unread,
Sep 29, 2005, 3:07:05 PM9/29/05
to
Dr John Stockton <repl...@merlyn.demon.co.uk> wrote:
> Timo Salmi <t...@uwasa.fi> posted :
> >119) How can I remove the leading zeros a non-negative integer?

> 119) How can I remove any leading zeros from an unsigned integer string?

Much better, thanks. I am going to use
119} How can I remove any leading zeros from an unsigned integer?

(The "string" is implied enough)

0 new messages