Thousand integer delimiters

Say I have this in my batch file:

set var1=12345 

And I want to add commas to the first variable (var1) and save it in another variable (var2) .

This means that the second variable (var2) should have this value: 12.345 .

So, if the first variable (var1) had this value: 123456789, the second value (var2) should be 123 456 789.

Any ideas?

EDIT:

I do not need PowerShell commands because they do not work for me. Please write without PowerShell.

+2
source share
7 answers

One more!

 @echo off setlocal EnableDelayedExpansion set "var1=%1" echo First variable: %var1% set "var2=" set "sign=" if "%var1:~0,1%" equ "-" set "sign=-" & set "var1=%var1:~1%" for /L %%i in (1,1,4) do if defined var1 ( set "var2=,!var1:~-3!!var2!" set "var1=!var1:~0,-3!" ) set "var2=%sign%%var2:~1%" echo Second variable: %var2% 

If you want to increase the number of digits, simply increase the number of groups in the for command to a value greater than 4.

+4
source

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 
+2
source

A package is not a good language for doing this kind of complex string manipulation. For most people, I would recommend using PowerShell for hard work.

 for /f %%a in ('powershell -c "'{0:N0}' -f %var1%"') do set var2=%%a 

If you need a purely batch solution, you can do iteratively with much more code. This example works with 32-bit integers. For large numbers or floating point numbers, see @dbenham's answer.

thousands.cmd

 @echo off :: in: Integer value (must fit into 32 bits) :: Out: Prints that value with commas after each 3 digits setlocal set /a num = %1 set output= :: Check for trivial zero case and any non-number string input if {%num%}=={0} ( echo 0 goto :eof ) :nextgroup if %num:-=% LSS 1000 goto :lastgroup set output=,%num:~-3%%output% set /a num = %num% / 1000 goto :nextgroup :lastgroup set output=%num%%output% endlocal && echo.%output% 

Examples:

 > thousands.cmd 1000 1,000 > thousands.cmd -876543210 -876,543,210 > thousands.cmd 0 0 > thousands.cmd -512 -512 > thousands.cmd fred 0 
+1
source
 set var2=%var1:~0,-3%,%var1:~-3% 
+1
source

SET var2=%var1%,5 should complete the task.

0
source

quick and easy solution. You can save this as a package and call it with your number as an argument, or embed it in any script that you make.

explanation:

if the input value of val is greater than 999, then set "newval" as a comma, the last three digits of "val", and then trim the last three digits of val.
loop until the val value is more than 3 digits, output the output.

 setlocal set val=%1 :loop if %val% GTR 999 (set newval=,%val:~-3%%newval% & set val=%val:~0,-3% goto loop ) echo %val:~-3%%newval% 

and if you really need to deal with negative numbers, use this instead.

 setlocal set val=%1 if %val% LSS 0 ( set "sign=-" & set "val=%val:~1%" ) :loop if %val% GTR 999 (set newval=,%val:~-3%%newval% & set val=%val:~0,-3% goto loop ) echo %sign%%val:~-3%%newval% 

Of course, this does not handle bad input, but it is short and pleasant.

0
source

A little late for the party that I know, but it can be useful for those who are late for this topic, like me. This method uses the basic methods of replacing SET. It also supports negative integers with a leading [-] sign. This is a static solution that uses an arbitrary maximum input digit size, but it can be changed as required. For those interested in performance, this is faster than FOR loop solutions.

 :: Sample code is for max 21 (-20) digit numbers :: For larger numbers increase pad size and extend the "preset commas" instruction :: with leading " %var1:~-NN,3%, " where NN increments by 3 :: The only limit would seem to be variable VAR2 maximum size :: Note: no size check nor numeric integrity is applied, if input size exceeds :: design max top digits are simply lost rem Left pad input with 20 [max-1] characters of your choice [except comma] rem 12345678901234567890 set var1=####################%1 rem preset comma separators set var2=%var1:~-21,3%,%var1:~-18,3%,%var1:~-15,3%,%var1:~-12,3%,%var1:~-9,3%,%var1:~-6,3%,%var1:~-3% rem remove redundant chars set var2=%var2:#,=% set var2=%var2:#=% rem only needed if -nn negative numbers are to be supported set var2=%var2:-,=-% ECHO %1 ==^> %var2% 
0
source

All Articles