Hi,
I'm looking for a away to format a number from 1234567890 to 1,234,567,890
I already tried [format "%5.3f" $number] and similar versions, but I cannot find the right way.
thanks for your help,
tueb
%commas 123456789 ;# naturally, it defaults for use with large decimal integers
123,456,789
Code: Select all
proc {commas} {var {num 3} {char ","}} {
set len [string length $var]
set first [expr $len - $num]
set x ""
while { $len > 0} {
# grab left num chars
set lef [string range $var $first end]
if {[string length $x] > 0} {
set x "${lef}$char${x}"
} else {
set x ${lef}
}
# grab everything except left num chars
set var [string range $var 0 [expr $first -1]]
set len [string length $var]
set first [expr $len - $num]
}
return $x
}