This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

[soved]formatting numbers

Help for those learning Tcl or writing their own scripts.
Post Reply
t
tueb
Halfop
Posts: 76
Joined: Thu Oct 04, 2007 6:09 am
Location: #quiz.de @ irc.gamesurge.net
Contact:

[soved]formatting numbers

Post by tueb »

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
Last edited by tueb on Thu Feb 19, 2009 5:26 am, edited 1 time in total.
#Quiz.de @ irc.GameSurge.net
JavaChat
User avatar
TCL_no_TK
Owner
Posts: 509
Joined: Fri Aug 25, 2006 7:05 pm
Location: England, Yorkshire

Post by TCL_no_TK »

Check out the "commas" proc from http://wiki.tcl.tk/1591
%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
 }
t
tueb
Halfop
Posts: 76
Joined: Thu Oct 04, 2007 6:09 am
Location: #quiz.de @ irc.gamesurge.net
Contact:

Post by tueb »

it works,


thx
#Quiz.de @ irc.GameSurge.net
JavaChat
Post Reply