A bit hard to do with a clean batch. Here is an efficient solution that uses the function : strlen to determine the length of a string. Function: showThousands will work with any integer, positive or negative, up to 8191 digits. The function only changes values ββthat do not necessarily begin with - , and then the remainder consists of only numbers.
@echo off setlocal enableDelayedExpansion for %%N in ( 1 12 123 1234 12345 123456 1234567 123456789 1234567890 12345678901234567890 -12345678901234567890 fred 123456.789 ) do ( set input=%%N call :showThousands input output echo !input! --^> !output! ) exit /b :showThousands inVar outVar setlocal enableDelayedExpansion set num=!%~1! set "sign=" if %num:~0,1% equ - ( set "sign=-" set "num=%num:~1%" ) for /f "delims=0123456789" %%A in ("%num%") do goto :showThousandsReturn call :strlen len num if %len% leq 3 goto :showThousandsReturn set /a end=len%%3 if %end% equ 0 set /a end=3 set /a start=(len-4)/3*3+end for /l %%N in (%start% -3 %end%) do set "num=!num:~0,%%N!,!num:~%%N!" :showThousandsReturn endlocal & set "%~2=%sign%%num%" exit /b :strlen <resultVar> <stringVar> ( setlocal EnableDelayedExpansion set "s=!%~2!#" set "len=0" for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do ( if "!s:~%%P,1!" NEQ "" ( set /a "len+=%%P" set "s=!s:~%%P!" ) ) ) ( endlocal set "%~1=%len%" exit /b )
- OUTPUT -
1 --> 1 12 --> 12 123 --> 123 1234 --> 1,234 12345 --> 12,345 123456 --> 123,456 1234567 --> 1,234,567 123456789 --> 123,456,789 1234567890 --> 1,234,567,890 12345678901234567890 --> 12,345,678,901,234,567,890 -12345678901234567890 --> -12,345,678,901,234,567,890 fred --> fred 123456.789 --> 123456.789
source share