Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Is it possible to take a variable with a numerical value for instance and add a comma every 3 characters moving backward? How could this be done?
ie: "20000" would change to "20,000"

A really handy command for this type of thing is Environment variable substitution. Basically you can use it to carve up your variables.
You specify it as your variable(what ever it may be) followed by a colon, then a ~, then a number then possibly a comma then another number "%yourvariable:~x,x%"
It can be used as follows:
%yourvariable:~1% - This would be all of your variable starting from character 2, as the first character is :~0 and if you don't specify a ending value the remainder of the variable is assumed.%yourvariable:~1,6% - This would be all of yourvariable starting from character 2, ending at character 7.
%yourvariable:~0,-3% - This would be all of yourvariable starting from the first character, excluding the last three characters.
%yourvariable:~-3% - This would be the last three characters of your variable.
for more information go to the command line and type "set /?" (I think it will be around the third page)
As for your problem, the below seemed to work for me though it's a bit of a quick job, so I can't say it's error free or optimal. You may want to clean it up if you actually use it. Also it will work with letters as well as numbers.
[edit fixed 3 or less characters, twice]
set count=0
set yourvariable=20000
set tempvariable=%yourvariable%
set splitvariable=%yourvariable%
::your variables length
:length
if not defined tempvariable goto nextthing
set /a count+=1
set tempvariable=%tempvariable:~1%
goto length::checks we have enough characters
::or any at all
:nextthing
if %count% == 0 goto nothg
if %count% leq 3 goto lessthan3
goto splitter::Set the last 3 characters of splitvariable,
::a comma and whatever is already in
::outputvar into outputvar.
::taking the last three characters off
::splitvariable, then taking three
::off the count, if the count becomes less
::than three then it jumps to output
:splitter
if %count% lss 3 goto output
set outputvar=%splitvariable:~-3%,%outputvar%
set splitvariable=%splitvariable:~0,-3%
set /a count-=3
goto splitter::If our initial number of characters
::isn't divisable
::buy three then splitvariable
::still holds one or two digits
::setting it to its self and a comma
::at the end finishes the job.
::outputvar has an extra comma at the end
::we don't want, so we get rid of it.
:output
if defined splitvariable set splitvariable=%splitvariable%,
echo %splitvariable%%outputvar:~0,-1%
pause
goto end
:lessthan3
echo %yourvariable%
pause
goto end
:nothg
Echo yourvariable was not set
pause
:end
set count=
set yourvariable=
set tempvariable=
set splitvariable=
set outputvar=

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |