Subject: Re: Format Numbers with Comma Seperators - DN [1]


Dan Kuchler <kuchler@ajubasolutions.com> - 14 Jul 2000 - comp.lang.tcl

 bmark987@my-deja.com wrote:
 >
 > In article <396FC71D.98831874@ajubasolutions.com>,
 >   Jeffrey Hobbs <jeffrey.hobbs@ajubasolutions.com> wrote:
 > > bmark987@my-deja.com wrote:
 > > >  How does one take a string variable with an integer value like
 > > > "1473376"
 > > > and convert it to
 > > > "1,473,376"
 > > > to enhance readability on screen for columns of numbers?
 > >
 > > One magic regsub coming up... (for 8.1.1+):
 > >
 > > # commify --
 > > #   puts commas into a decimal number
 > > # Arguments:
 > > #   num               number in acceptable decimal format
 > > #   sep               separator char (defaults to English format ",")
 > > # Returns:
 > > #   number with commas in the appropriate place
 > > #
 > > proc commify {num {sep ,}} {
 > >     while {[regsub {^([-+]?\d+)(\d\d\d)} $num "\\1$sep\\2" num]} {}
 > >     return $num
 > > }
 > >
 > > --
 > >    Jeffrey Hobbs                     The Tcl Guy
 > >    hobbs at ajubasolutions.com       Ajuba Solutions (née Scriptics)
 > >
 >
 > Sure there isn't a typo on the above? It looks terriffic but I get
 > error when I try to run it
 >

 Be sure that you are running a version over version 8.1.1 and then make
 sure that everything is on the line that is intended (make sure your newsreader
 isn't wrapping things.)

 The while statement needs to all be on one line.
 -----------------------------------------------------

 while {[regsub {^([-+]?\d+)(\d\d\d)} $num "\\1$sep\\2" num]} {}

 --------------------------------------------------
 should all be on one line.

 Then the next line should be
 --------------------------------------------------

 return $num

 --------------------------------------------------

 This is just a two line proc.

 From the error you got, it looks like the body (in this case {} )
 of the while loop had been pushed onto the next line by your newsreader.

 The procedure does work correctly on 8.3 (and should work correctly on
 all 8.1.1+ versions of tcl).

 --Dan

Last modified
2000-07-20

(195.108.246.52)

Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated