egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[SOLVED] Add Commas to a Number

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
Fire-Fox
Master


Joined: 23 Sep 2006
Posts: 270
Location: /dev/null

PostPosted: Tue Feb 28, 2012 2:06 pm    Post subject: [SOLVED] Add Commas to a Number Reply with quote

Hey!

How do i add commas to a database output so its more readable ? Smile
_________________
GreatZ
Fire-Fox | Denmark

Scripts: Relay | Store Text | TvMaze


Last edited by Fire-Fox on Wed Feb 29, 2012 12:54 pm; edited 1 time in total
Back to top
View user's profile Send private message MSN Messenger
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Tue Feb 28, 2012 6:33 pm    Post subject: Reply with quote

I can't think of any packages that will do this for you right now, but then again, the procedure isn't that hard;
Pick out pieces of three digits, and insert the separator of your choice inbetween. The only tricky part is to decide whether the left-most block should be 1, 2, or 3 digits long - as this is determined by the length of the whole string.

Provided you are dealing with integer, the following should probably work:
Code:
proc nFormat {string} {
  set len [expr [string length $string] - 1]
  set i [expr $len % 3]
  set out [string range $string 0 $i]
  while {$i < $len} {
    append out ",[string range $string [incr i] [incr i 2]]"
  }
  return $out
}

If you are dealing with floating-point values, you'd have to separate the decimal part of the value before iterating through the string.
_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
speechles
Revered One


Joined: 26 Aug 2006
Posts: 1398
Location: emerald triangle, california (coastal redwoods)

PostPosted: Tue Feb 28, 2012 9:02 pm    Post subject: Reply with quote

There is actually more to it than simply three digits and a comma. What appears to be the best all around answers was..
Code:
proc commify number {regsub -all \\d(?=(\\d{3})+([regexp -inline {\.\d*$} $number]$)) $number {\0,}}


set comma_number [commify "123456128213123412.1234"]

See here: http://wiki.tcl.tk/5000
_________________
speechles' eggdrop tcl archive
Back to top
View user's profile Send private message
Fire-Fox
Master


Joined: 23 Sep 2006
Posts: 270
Location: /dev/null

PostPosted: Wed Feb 29, 2012 12:15 pm    Post subject: Reply with quote

Thanks got i working Very Happy
_________________
GreatZ
Fire-Fox | Denmark

Scripts: Relay | Store Text | TvMaze
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber