formatted output with commas

var=12345
echo $var >>>12345

printf "%8.1f \n" $var >>> 12345.0

How to get this as 12,345?
I suppose I could break into sections by dividing by 1000 or 1000000.
But, is the a trick to this?

in this instance you could use:

 printf "%8.1f \n" $var | sed 's/...\./,&/g'

But for something with more than 5 characters or less than 4 this wouldn't work.

The data to report is a counter; my guess is normally between one and seven digits. Thus I could want to see

1,234,567
  123,456
   12,345
    1,234
      123
       12
        1

I could be reporting in any of the above layouts - comma at every xxx, right-justified. I fear that I may be forced to break my variable counter into sections and handle three characters at a time.

sed to add commas in to numbers:

 echo 1234567 | sed -e :a -e 's/\([0-9][0-9]*\)\([0-9][0-9][0-9]\)/\1,\2/;ta'

HTH

Someday, I will learn what all of those codes with the 'sed' mean :o