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

Is There A Way To Format A Number?

2,299 views
Skip to first unread message

(PeteCresswell)

unread,
Feb 6, 2013, 1:26:29 PM2/6/13
to
I'm computing an elapsed time in a .CMD file and would like to format it
using commas for thousands.

e.g. 129890 ==> 129,890

I'm not optimistic.... is there any hope?

It's not exactly a religious issue... but seems like a significant
convenience for whoever has to read the screen.

Tangentially, I've stumbled on a manual for "WIL" (Windows Interface
Language"). Is this a third-party thing? Or is it built in to
WIndows.... I'm thinking maybe there's some hope there, but I don't want
to waste a lot of time...
--
Pete Cresswell

Tom Lavedas

unread,
Feb 6, 2013, 1:50:16 PM2/6/13
to
There is no intrinsic batch functionality to format the number. I can think of a way to do it with a bathch routine, but it would be a bit combersome. VBScripting has such a function, so I'd suggest using that capability through a hybrid VBS/batch procedure ...

:: FormatNumb.cmd
@echo off
echo wsh.echo formatnumber(%1,0,,true)> %temp%.\T.vbs
for /f "delims=" %%R in (
'cscript.exe //nologo %temp%.\T.vbs'
) do set %~n0=%%R
del %temp%.\T.vbs

The formatted number string is returned in an environment variable that matches the name of the batch procedure (in this case, FormatNumb).

As far as the second question - I've never heard of WIL.
_____________________________
Tom Lavedas

John Gray

unread,
Feb 6, 2013, 2:01:59 PM2/6/13
to
Although this is totally dependent on the number consisting of six digits, the following could perhaps be a start for something rather more sophisticated?

set num=129890
set num=%num:~0,3%,%num:~-3%
echo %num%

Frank Westlake

unread,
Feb 6, 2013, 3:17:54 PM2/6/13
to
On 2013-02-06 10:26, (PeteCresswell) wrote:
> I'm computing an elapsed time in a .CMD file and would like to format it
> using commas for thousands.

There is not a built-in command so here is a script. It seems to me
this could be simpler. An example:

Call :formatInteger result 123456789

Frank

:: BEGIN SCRIPT :::::::::::::::::::::::::::::::::::::::::::::::::::::
:: FormatInteger.cmd
:: From the desk of Frank P. Westlake, 2013-02-06
:: Written on Windows 8.
@Echo OFF
SetLocal EnableExtensions EnableDelayedExpansion
Set "raw=%~1"
If NOT DEFINED raw Set "raw=1234567890"

Call :formatInteger result %raw%
set result

Call :formatInteger result %raw% "."
set result

Goto :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:formatInteger <returnVariableName> <integer> [separator]
SetLocal EnableExtensions EnableDelayedExpansion
Set "raw=%~2"
Set "answer="
Set "separator=%~3"
If NOT DEFINED separator Set "separator=,"
For /F "delims=:" %%a in (
'(ECHO;%2^& Echo.NEXT LINE^)^|FindStr /O "NEXT LINE"'
) Do Set /A "length=%%a-3"
For /L %%i in (!length!, -3, 1) Do (
IF DEFINED answer (
Set "answer=!raw:~-3!!separator!!answer!"
) Else (
Set "answer=!raw:~-3!"
)
Set "raw=!raw:~0,-3!"
)
::If DEFINED remainder Set "answer=!remainder!,!answer!"
EndLocal & Set "%~1=%answer%"
:: END SCRIPT ::::::::::::::::::::::::::::::::::::::::::::::::::::

Frank

Frank Westlake

unread,
Feb 6, 2013, 3:25:36 PM2/6/13
to
On 2013-02-06 12:17, Frank Westlake wrote:
> ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
> :formatInteger <returnVariableName> <integer> [separator]

This might have problems. I just inserted it in a script which displays
some numbers and there were errors. Bill's solution will probably be
more reliable.

Frank

Tom Lavedas

unread,
Feb 6, 2013, 4:10:50 PM2/6/13
to
Bill's solution? I haven't seen that one.

Here is my 'pure' batch solution ...

:: FormatNumb.cmd
@echo off & setlocal
set "formatNumb="
set remainder=%1

:Loop
set /a thousand=remainder-remainder/1000*1000
set /a remainder=remainder/1000
set Formatnumb=%thousand%,%FormatNumb%
if %remainder% NEQ 0 goto :Loop

echo %FormatNumb:~0,-1%
______________________________
Tom Lavedas

Frank Westlake

unread,
Feb 6, 2013, 5:05:57 PM2/6/13
to
On 2013-02-06 10:26, (PeteCresswell) wrote:
> I'm computing an elapsed time in a .CMD file and would like to format it
> using commas for thousands.

Here's another. This and my previous script both work fine in the demo
but and when incorporated into another script the numbers are
incorrectly formatted. It might be the fault of the script that I'm
putting the subroutines in, not of the subroutines themselves.

Interestingly, both these subroutines have the same total CPU times.

Also, both these subroutines will handle numbers of any length, not
just 32-bit numbers.

Frank

:: BEGIN SCRIPT :::::::::::::::::::::::::::::::::::::::::::::::::::::
:: FormatInteger.cmd
:: From the desk of Frank P. Westlake, 2013-02-06
:: Written on Windows 8.
@Echo OFF
SetLocal EnableExtensions EnableDelayedExpansion
Set "raw=%~1"
If NOT DEFINED raw Set "raw=1234567890"

Call :formatInteger result %raw%
set result

Call :formatInteger result %raw% "."
set result

Goto :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:formatInteger <returnVariableName> <integer> [separator]
SetLocal EnableExtensions EnableDelayedExpansion
Set "answer=" & Set "raw=%~2" & Set "separator=%~3" & Set "n=0"
If NOT DEFINED separator Set "separator=,"
For %%a in (0 1 2 3 4 5 6 7 8 9) Do Set "raw=!raw:%%a=%%a !"
For %%a in (!raw!) Do (
If !n! EQU 0 Set "answer=!answer!!separator!"
Set /A "n=(n+1)%%3"
Set "answer=!answer!%%a"
)
EndLocal & Set "%~1=%answer:~0,-1%"
:: END SCRIPT ::::::::::::::::::::::::::::::::::::::::::::::::::::


(PeteCresswell)

unread,
Feb 6, 2013, 5:09:12 PM2/6/13
to
Per Tom Lavedas:
>
>Bill's solution? I haven't seen that one.

+1
--
Pete Cresswell

Frank Westlake

unread,
Feb 6, 2013, 5:26:25 PM2/6/13
to
Skip this one; it suffered from the "failure to terminate a subroutine
with GOTO :EOF error" and continued on to the previous :formatNumber
that I had written. This one will require a few more lines to fix so I
won't bother with it.

Frank

Frank Westlake

unread,
Feb 6, 2013, 5:53:40 PM2/6/13
to
On 2013-02-06 12:17, Frank Westlake wrote:
> ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
> :formatInteger <returnVariableName> <integer> [separator]
> SetLocal EnableExtensions EnableDelayedExpansion
> Set "raw=%~2"
> Set "answer="
> Set "separator=%~3"
> If NOT DEFINED separator Set "separator=,"
> For /F "delims=:" %%a in (
> '(ECHO;%2^& Echo.NEXT LINE^)^|FindStr /O "NEXT LINE"'
> ) Do Set /A "length=%%a-3"
> For /L %%i in (!length!, -3, 1) Do (
> IF DEFINED answer (
> Set "answer=!raw:~-3!!separator!!answer!"
> ) Else (
> Set "answer=!raw:~-3!"
> )
> Set "raw=!raw:~0,-3!"
> )
> ::If DEFINED remainder Set "answer=!remainder!,!answer!"
> EndLocal & Set "%~1=%answer%"
> :: END SCRIPT ::::::::::::::::::::::::::::::::::::::::::::::::::::

It was failing in another script because I had quoted the number and the
string length calculated the two extra characters. This is better:

:: BEGIN SCRIPT :::::::::::::::::::::::::::::::::::::::::::::::::::::
:formatInteger <returnVariableName> <integer> [separator]
SetLocal EnableExtensions EnableDelayedExpansion
Set "answer=" & Set "raw=%~2" & Set "separator=%~3"
If NOT DEFINED separator Set "separator=,"
For /F "delims=:" %%a in ('(ECHO;%~2^& Echo.X^)^|FindStr /O "X"'
) Do Set /A "length=%%a-3"
For /L %%i in (!length!, -3, 1) Do (
Set "answer=!raw:~-3!!separator!!answer!"
Set "raw=!raw:~0,-3!"
)
EndLocal & Set "%~1=%answer:~0,-1%"
Goto :EOF
:: END SCRIPT ::::::::::::::::::::::::::::::::::::::::::::::::::::

Frank

Zaidy036

unread,
Feb 6, 2013, 6:49:38 PM2/6/13
to
assuming number is always at least 4 digits long then KISS it:
SET nmbr=123456
SET nmbr=%nmbr:~0,3%,%nmbr:~3%

billious

unread,
Feb 6, 2013, 7:29:05 PM2/6/13
to
On 7/02/2013 06:09, (PeteCresswell) wrote:
> Per Tom Lavedas:
>>
>> Bill's solution? I haven't seen that one.
>
> +1
>

@ECHO OFF
SETLOCAL
SET number=%1
CALL :FORMAT %number%
ECHO %number% -^>%formatted%
GOTO :eof
::
:FORMAT
@ECHO off
(SET formatted=)
SET _number=%1
:formloop
SET formatted=%_number:~-3%%formatted%%
set _number=%_number:~0,-3%
IF DEFINED _number SET formatted=,%formatted%&GOTO formloop
GOTO :eof

Standard rules - lines are indented two spaces; any line not indented
two spaces has been wrapped and will need to be rejoined.

Stating the obvious: the first 8 lines are a test procedure. The part
after the label :FORMAT could be saved (as 'format.bat' for instance) -
hence the seemingly-superfluous @ECHO OFF

I'm confused...


Bob

unread,
Feb 6, 2013, 11:18:04 PM2/6/13
to
On 2/6/2013 1:26 PM, (PeteCresswell) wrote:
> I'm computing an elapsed time in a .CMD file and would like to format it
> using commas for thousands.
Greetings Pete,

Is SED an option for you to use?

::GNU SED Version 4.2.1 & WIN7
@echo off
setlocal

set num=1234567890

For /f "tokens=*" %%b in (
'Echo %num% ^| SED -e ":a;s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta"'
) do (
set comma_num=%%b
)

Echo The number without commas is %num%
Echo The comma seperated number is %comma_num%
pause
::

I did not do a ton of testing but the above will not handle a number
like 1234.5678

Later.

Todd Vargo

unread,
Feb 7, 2013, 12:12:08 AM2/7/13
to
Here is my go at it. Obviously, one can modify to receive the number as
a parameter, but I leave that for the scholar.

@echo off
set "num=1234567890.1234567890"

:groupnum
setlocal enabledelayedexpansion
for /f "tokens=1,2 delims=." %%a in ("%num%") do (
set "int=*%%a"
set "fnum=.%%b"
)
set "sep=0"
for /l %%i in (1,1,100) do (
set "n=!int:~-%%i,1!"
if "!n!" equ "*" goto :donegroupnum
set /a sep+=1
if !sep! equ 4 (
set "fnum=!n!,!fnum!"
set "sep=1"
) else (
set "fnum=!n!!fnum%!
)
)
:donegroupnum

echo %num%
echo %fnum%


--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

Todd Vargo

unread,
Feb 7, 2013, 12:31:28 AM2/7/13
to
On 2/7/2013 12:12 AM, Todd Vargo wrote:

>
> Here is my go at it. Obviously, one can modify to receive the number as
> a parameter, but I leave that for the scholar.
>
> @echo off
> set "num=1234567890.1234567890"
>
> :groupnum
> setlocal enabledelayedexpansion
> for /f "tokens=1,2 delims=." %%a in ("%num%") do (
> set "int=*%%a"
> set "fnum=.%%b"
> )
> set "sep=0"
> for /l %%i in (1,1,100) do (
> set "n=!int:~-%%i,1!"
> if "!n!" equ "*" goto :donegroupnum
> set /a sep+=1
> if !sep! equ 4 (
> set "fnum=!n!,!fnum!"
> set "sep=1"
> ) else (
> set "fnum=!n!!fnum%!
> )
> )
> :donegroupnum

endlocal&set "fnum=%fnum%"

I forgot to include the cleanup line here.

Bob

unread,
Feb 7, 2013, 12:58:14 AM2/7/13
to
On 2/7/2013 12:12 AM, Todd Vargo wrote:
> Here is my go at it.

Greetings Todd,

Nice script and so much better than my SED lines which would not handle
a line like: set "num=012abc345.6789" which your script does.

I threw your file a few more curve balls and it performed as expected.

Later.




foxidrive

unread,
Feb 7, 2013, 1:56:56 AM2/7/13
to
That works well, Bob. :)

The number without commas is 25456437547867856891234567890
The comma seperated number is 25,456,437,547,867,856,891,234,567,890



--
foxi

foxidrive

unread,
Feb 7, 2013, 2:03:26 AM2/7/13
to
On 7/02/2013 4:58 PM, Bob wrote:
> On 2/7/2013 12:12 AM, Todd Vargo wrote:
>> Here is my go at it.
>
> Greetings Todd,
>
> Nice script and so much better than my SED lines which would not handle
> a line like: set "num=012abc345.6789" which your script does.

I reckon that if you are passing a number to have commas added then it is quite reasonable to expect that
there is no hexadecimal or anything other than a pure base 10 number.


--
foxi

Todd Vargo

unread,
Feb 7, 2013, 6:00:16 AM2/7/13
to
Oops, I forgot to include the disclaimer that input validation is also
left to the scholar. ;-)

Frank Westlake

unread,
Feb 7, 2013, 6:17:21 AM2/7/13
to
I took all the submissions and made then subroutines which accept a
variable name as parameter 1 and the number value as parameter 2. All
the scripts are copied below.

Here's the comparison:

Tom1 Tom2 Frank
Billious Todd
Read Operation Count 251 315 254
304 262
Read Transfer Count 240,901 256,346
243,812 252,180 245,622
Write Operation Count 1 0 0
0 0
Write Transfer Count 43 0 0
0 0
Kernel Mode Time 0:00.0468003 0:00.0312002
0:00.0312002 0:00.0156001 0:00.0468003
User Mode Time 0:00.0312002 0:00.0468003
0:00.0156001 0:00.0000000 0:00.0000000
Total CPU Time 0:00.0780005 0:00.0780005
0:00.0468003 0:00.0156001 0:00.0468003
Process Life Time 0:00.7175000 0:00.4989000
0:00.4675000 0:00.4833000 0:00.4523000
Times are in minutes:seconds.

Looks like the Billious routine is the most efficient.

Frank


::::::::::::::::::::::::::::::::::::::::::::::::::
::Billious.cmd
@Echo OFF
SetLocal EnableExtensions EnableDelayedExpansion
Set "raw=1234567890"
Call :Format result %raw%
set result
Goto :EOF

:FORMAT
@ECHO off
(SET formatted=)
SET _number=%~2
:formloop
SET formatted=%_number:~-3%%formatted%%
set _number=%_number:~0,-3%
IF DEFINED _number SET formatted=,%formatted%&GOTO formloop
Set "%~1=%formatted%"
GOTO :eof

::::::::::::::::::::::::::::::::::::::::::::::::::
::Frank.cmd
@Echo OFF
SetLocal EnableExtensions EnableDelayedExpansion
Set "raw=1234567890"
Call :formatInteger result %raw%
set result
Goto :EOF

:formatInteger <returnVariableName> <integer> [separator]
SetLocal EnableExtensions EnableDelayedExpansion
Set "answer=" & Set "raw=%~2" & Set "separator=%~3"
If NOT DEFINED separator Set "separator=,"
For /F "delims=:" %%a in ('(ECHO;%2^& Echo.X^)^|FindStr /O "X"'
) Do Set /A "length=%%a-3"
For /L %%i in (!length!, -3, 1) Do (
Set "answer=!raw:~-3!!separator!!answer!"
Set "raw=!raw:~0,-3!"
)
EndLocal & Set "%~1=%answer:~0,-1%"
Goto :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::
::Todd.cmd
@Echo OFF
SetLocal EnableExtensions EnableDelayedExpansion
Set "raw=1234567890"
Call :groupnum result %raw%
set result
Goto :EOF

:groupnum
setlocal enabledelayedexpansion
Set "num=%~2"
for /f "tokens=1,2 delims=." %%a in ("%num%") do (
set "int=*%%a"
set "fnum=.%%b"
)
set "sep=0"
for /l %%i in (1,1,100) do (
set "n=!int:~-%%i,1!"
if "!n!" equ "*" goto :donegroupnum
set /a sep+=1
if !sep! equ 4 (
set "fnum=!n!,!fnum!"
set "sep=1"
) else (
set "fnum=!n!!fnum%!
)
)
:donegroupnum
endlocal&set "%~1=%fnum%"
Goto :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::
::Tom1.cmd
@Echo OFF
SetLocal EnableExtensions EnableDelayedExpansion
Set "raw=1234567890"
Call :FormatNumb result %raw%
set result
Goto :EOF

:FormatNumb
@echo off
echo wsh.echo formatnumber(%~2,0,,true)> %temp%.\T.vbs
for /f "delims=" %%R in (
'cscript.exe //nologo %temp%.\T.vbs'
) do set result=%%R
del %temp%.\T.vbs
EndLocal & Set "%~1=%result%"
Goto :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::
::Tom2.cmd
@Echo OFF
SetLocal EnableExtensions EnableDelayedExpansion
Set "raw=1234567890"
Call :FormatNumb result %raw%
set result
Goto :EOF

:FormatNumb
@echo off & setlocal
set "formatNumb="
set remainder=%~2
:Loop
set /a thousand=remainder-remainder/1000*1000
set /a remainder=remainder/1000
set Formatnumb=%thousand%,%FormatNumb%
if %remainder% NEQ 0 goto :Loop

EndLocal & Set "%~1=%FormatNumb:~0,-1%"
Goto :EOF

Frank Westlake

unread,
Feb 7, 2013, 6:23:16 AM2/7/13
to
Here's the comparison:

Tom1 Tom2 Frank
Billious Todd
Read Operation Count 251 315 254 304
262
Read Transfer Count 240,901 256,346 243,812 252,180
245,622
Write Operation Count 1 0 0 0
0
Write Transfer Count 43 0 0 0
0
Kernel Mode Time 0:00.0468003 0:00.0312002 0:00.0312002
0:00.0156001 0:00.0468003
User Mode Time 0:00.0312002 0:00.0468003 0:00.0156001
0:00.0000000 0:00.0000000
Total CPU Time 0:00.0780005 0:00.0780005 0:00.0468003
0:00.0156001 0:00.0468003
Process Life Time 0:00.7175000 0:00.4989000 0:00.4675000
0:00.4833000 0:00.4523000
Times are in minutes:seconds.

Frank

Todd Vargo

unread,
Feb 7, 2013, 8:41:37 AM2/7/13
to
What, you didn't test with decimals? ;-)

billious

unread,
Feb 7, 2013, 9:52:49 AM2/7/13
to
@ECHO OFF
SETLOCAL
CALL :FORMAT %1
ECHO %~1 -^>%formatted%
GOTO :eof
::
:FORMAT
@ECHO off
(SET formatted=)
:: Set sep1=thousands separator;sep2 to decimal groups-of-3 separator
:: and sep3 to decimal separator
SET sep1=,&(SET sep2= )&SET sep3=.
FOR /f "tokens=1,2delims=%sep3%" %%i IN ("%~1") DO SET
_number=%%i&SET _mantissa=%%j
:formloop
SET formatted=%_number:~-3%%formatted%%
set _number=%_number:~0,-3%
IF DEFINED _number SET formatted=%sep1%%formatted%&GOTO formloop
IF NOT DEFINED _mantissa GOTO :EOF
SET formatted=%formatted%%sep3%
:formmlp
SET formatted=%formatted%%_mantissa:~0,3%
SET _mantissa=%_mantissa:~3%
IF DEFINED _mantissa SET formatted=%formatted%%sep2%&GOTO formmlp
GOTO :eof

Dealing with a leading-zero-suppressed decimal is left as an exercise
for anyone interested...

Frank Westlake

unread,
Feb 7, 2013, 10:08:43 AM2/7/13
to
On 2013-02-07 05:41, Todd Vargo wrote:
> What, you didn't test with decimals? ;-)

I guess a complete procedure would also accept floating point numbers
of any base, allow the selection of a grouping character, and allow the
specification of the number of characters in each group. The radix
character need not be selectable because it would already exist in the
number if one is necessary.

Frank

:: BEGIN SCRIPT :::::::::::::::::::::::::::::::::::::::::::::::::::::
:: FormatInteger.cmd
:: From the desk of Frank P. Westlake, 2013-02-07
:: Written on Windows 8.
@Echo OFF
SetLocal EnableExtensions EnableDelayedExpansion
Set "raw=%~1"
If NOT DEFINED raw Set "raw=1234567890.0987654321"

Call :formatNumber result %raw%
set result

REM This number must be quoted because the radix character is a comma.
Call :formatNumber result "FEDCBA9876543210,E0D" ":" 4
set result

Goto :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:formatNumber <returnVariableName> <number> [separator [group size]]
SetLocal EnableExtensions EnableDelayedExpansion
Set "answer=" & Set "raw=%~2" & Set "separator=%~3" & Set "group=%~4"
If NOT DEFINED separator Set "separator=,"
If NOT DEFINED group Set "group=3"
For %%G in (-!group!) Do (
For /F "tokens=1,2 delims=,." %%a in ("%~2") Do (
Set "int=%%a"
Set "frac=%%b"
For /F "delims=:" %%c in ('(ECHO;%%~a^& Echo.NEXT LINE^)^|FindStr
/O "NEXT LINE"'
) Do Set /A "length=%%c-3"
For %%c in (!length!) Do Set "radix=!raw:~%%c,1!"
For /L %%i in (!length!, %%G, 1) Do (
Set "answer=!int:~%%G!!separator!!answer!"
Set "int=!int:~0,%%G!"
)
)
)
EndLocal & Set "%~1=%answer:~0,-1%%radix%%frac%"
Goto :EOF
:: END SCRIPT ::::::::::::::::::::::::::::::::::::::::::::::::::::

Frank Westlake

unread,
Feb 7, 2013, 10:50:31 AM2/7/13
to
The capabilities of these routines are not the same, nor were they in
the previous comparison, but here are the results:

Billious2 Frank2 Todd2
Read Operation Count 724 301 294
Read Transfer Count 453,866 272,314 252,992
Write Operation Count 0 0 0
Write Transfer Count 0 0 0
Kernel Mode Time 0:00.0936006 0:00.0312002 0:00.0780005
User Mode Time 0:00.1092007 0:00.0312002 0:00.0156001
Total CPU Time 0:00.2028013 0:00.0624004 0:00.0936006
Process Life Time 0:00.7325000 0:00.5453000 0:00.4991000
Times are in minutes:seconds.

::Billious2.cmd
:: BEGIN SCRIPT :::::::::::::::::::::::::::::::::::::::::::::::::::::
:: FormatInteger.cmd
:: From the desk of Frank P. Westlake, 2013-02-07
:: Written on Windows 8.
@Echo OFF
SetLocal EnableExtensions EnableDelayedExpansion
Set "raw=1234567890.0987654321"

Call :format result %raw%
set result

Call :format result FEDCBA9876543210.E0D
set result

Goto :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:FORMAT
@ECHO off
SetLocal EnableExtensions
(SET formatted=)
:: Set sep1=thousands separator;sep2 to decimal groups-of-3 separator
:: and sep3 to decimal separator
SET sep1=,&(SET sep2= )&SET sep3=.
FOR /f "tokens=1,2delims=%sep3%" %%i IN ("%~2") DO SET
_number=%%i&SET _mantissa=%%j
:formloop
SET formatted=%_number:~-3%%formatted%%
set _number=%_number:~0,-3%
IF DEFINED _number SET formatted=%sep1%%formatted%&GOTO formloop
IF NOT DEFINED _mantissa GOTO :EOF
SET formatted=%formatted%%sep3%
:formmlp
SET formatted=%formatted%%_mantissa:~0,3%
SET _mantissa=%_mantissa:~3%
IF DEFINED _mantissa SET formatted=%formatted%%sep2%&GOTO formmlp
EndLocal & Set "%~1=%formatted%"
GOTO :eof
:: END SCRIPT ::::::::::::::::::::::::::::::::::::::::::::::::::::

::Frank2.cmd
:: BEGIN SCRIPT :::::::::::::::::::::::::::::::::::::::::::::::::::::
:: FormatInteger.cmd
:: From the desk of Frank P. Westlake, 2013-02-07
:: Written on Windows 8.
@Echo OFF
SetLocal EnableExtensions EnableDelayedExpansion
::Todd2.cmd
@Echo OFF
SetLocal EnableExtensions EnableDelayedExpansion
Set "raw=1234567890"
Call :groupnum result %raw%
set result

REM This number must be quoted because the radix character is a comma.
Call :groupnum result FEDCBA9876543210.E0D

Frank Westlake

unread,
Feb 7, 2013, 11:00:10 AM2/7/13
to
On 2013-02-07 07:50, Frank Westlake wrote:
> The capabilities of these routines are not the same, nor were they in
> the previous comparison, but here are the results:

Oops, the scripts weren't equal. Here are two runs:

Billious2 Frank2 Todd2
Read Operation Count 652 293 293
Read Transfer Count 403,175 260,047 252,061
Write Operation Count 0 0 0
Write Transfer Count 0 0 0
Kernel Mode Time 0:00.1092007 0:00.0468003 0:00.0624004
User Mode Time 0:00.0468003 0:00.0312002 0:00.0156001
Total CPU Time 0:00.1560010 0:00.0780005 0:00.0780005
Process Life Time 0:00.7168000 0:00.5148000 0:00.5142000
Times are in minutes:seconds.

Billious2 Frank2 Todd2
Read Operation Count 652 293 293
Read Transfer Count 403,175 260,047 252,061
Write Operation Count 0 0 0
Write Transfer Count 0 0 0
Kernel Mode Time 0:00.0624004 0:00.0780005 0:00.0312002
User Mode Time 0:00.0468003 0:00.0000000 0:00.0624004
Total CPU Time 0:00.1092007 0:00.0780005 0:00.0936006
Process Life Time 0:00.6387000 0:00.5147000 0:00.4985000
Times are in minutes:seconds.

::Billious2.cmd
::Frank2.cmd
@Echo OFF
SetLocal EnableExtensions EnableDelayedExpansion
Set "raw=1234567890.0987654321"

Call :formatNumber result %raw%
set result

Call :formatNumber result "FEDCBA9876543210,E0D" ":" 4
set result

Goto :EOF

:formatNumber <returnVariableName> <number> [separator [group size]]
SetLocal EnableExtensions EnableDelayedExpansion
Set "answer=" & Set "raw=%~2" & Set "separator=%~3" & Set "group=%~4"
If NOT DEFINED separator Set "separator=,"
If NOT DEFINED group Set "group=3"
For %%G in (-!group!) Do (
For /F "tokens=1,2 delims=,." %%a in ("%~2") Do (
Set "int=%%a"
Set "frac=%%b"
For /F "delims=:" %%c in ('(ECHO;%%~a^& Echo.NEXT LINE^)^|FindStr
/O "NEXT LINE"'
) Do Set /A "length=%%c-3"
For %%c in (!length!) Do Set "radix=!raw:~%%c,1!"
For /L %%i in (!length!, %%G, 1) Do (
Set "answer=!int:~%%G!!separator!!answer!"
Set "int=!int:~0,%%G!"
)
)
)
EndLocal & Set "%~1=%answer:~0,-1%%radix%%frac%"
Goto :EOF

::Todd2.cmd
@Echo OFF
SetLocal EnableExtensions EnableDelayedExpansion
Set "raw=1234567890.0987654321"

Call :groupnum result %raw%
set result

vit...@yahoo.com

unread,
Feb 7, 2013, 7:53:47 PM2/7/13
to
Hey, Frank!

I rummaged around on my old hard drive and found the following from 2004:

@echo off
setlocal enabledelayedexpansion
::
:: inserts commas in number of any length
::
:: examples: 12345678 becomes 12,345,678
:: $12345678.99 becomes $12,345,678.99
:: 12345678.00172 becomes 12,345,678.00172
::
:: the result is returned in environment variable %~n0
::
:: to DISPLAY the result, enter "." as the second parameter
::

set S001=%1

:: remove leading dollar sign, if any
if %S001:~0,1%==$ (
set currency=yes
set S001=%S001:~1%
)

:: remove fractional portion, if any
for /f "tokens=1,2 delims=." %%a in ("%S001%") do set fraction=%%b
if defined fraction set S001=!S001:.%fraction%=!

set S002=%S001%
set length=0
:zsize
set /a length+=1
set S002=%S002:~1%
if defined S002 goto :zsize
set /a ctr=-1
set out=
for /l %%a in (%length%,-1,0) do call :digit %%a
if "%out:~0,1%"=="," set out=%out:~1%
if defined fraction set out=%out%.%fraction%
if defined currency set out=$%out%
if "%2"=="." echo %out%
endlocal&set %~n0=%out%&goto :EOF

:digit
set pos=%1
set /a ctr+=1
if %ctr% EQU 3 set comma=,
set digit=!S001:~%pos%,1!
set out=%comma%%digit%%out%
if defined comma (
set comma=
set ctr=0
)
goto :EOF

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is my first attempt to post code via the Google web
interface - hope it comes out OK format-wise.

Phil Robyn
# probyn (at) berkeley (dot) edu #

Frank Westlake

unread,
Feb 8, 2013, 4:38:53 AM2/8/13
to
On 2013-02-07 16:53, vit...@yahoo.com wrote:
> I rummaged around on my old hard drive and found the following from 2004:

I don't remember seeing that particular script but the style looks
familiar.

> This is my first attempt to post code via the Google web
> interface - hope it comes out OK format-wise.

It looked fine -- no google-wrap. There are free Usenet servers. The
one I use (albasani.net) doesn't stick buggers on the bottom of the
message.

Frank

vit...@yahoo.com

unread,
Feb 8, 2013, 2:44:22 PM2/8/13
to
Hi, Frank,

Thanks for the reference to albasani.net. Looks promising indeed. Maybe
later today I'll apply for an account there. I'm finally gaining some
momentum with this new (to me) Windows 7 stuff. . . .

Phil
^ probyn (at) berkeley (dot) edu ^

Dr J R Stockton

unread,
Feb 13, 2013, 2:18:40 PM2/13/13
to
In alt.msdos.batch.nt message <a0be22b7-52d8-4355-9379-13e72ceb2720@goog
legroups.com>, Wed, 6 Feb 2013 13:10:50, Tom Lavedas
<tglb...@verizon.net> posted:
Not, I suppose, a tested one.
Inspection tests, and testing shows, that it will fail to give the
presumably-expected format if the value of any of the intended three-
digit sections is less than 100. One fix should be to add 1000 and take
the last three characters; there may be a better one.

--
(c) John Stockton, nr London UK. Mail, see homepage. DOS 3.3, 6.20; WinXP, 7.
Web <http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.
0 new messages