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