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.

commas

Old posts that have not been replied to for several years.
Locked
B
BrollY
Voice
Posts: 36
Joined: Wed Apr 23, 2003 9:28 pm
Location: H07 L471N4 L4ND (Puerto Rico)

commas

Post by BrollY »

was wondering if anyone had a code to put commas in really long numbers, such as 1000000 would be turned into 1,000,000. i currently have a piece of code that does this for me that i got from a site, but its kinda "not working" right with some of my scripts.
Yo momma's so fat she can be in the past, present, n future all at once
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

I am sure there is a regsub procedure on the forum from many many moons ago, that took care of this in a quick and simple step. I sugest you search for this, however, here is a normal version.

Code: Select all

proc groupdigits {in {sep {,}}} {
  set out ""
  set in [split $in {}]
  while {[llength $in]} {
    set out "${sep}[join [lrange $in end-2 end] {}]${out}"
    set in [lreplace $in end-2 end]
  }
  return [string trimleft $out $sep]
}
Use it like so

Code: Select all

groupdigits 10000
10,000
groupdigits 1000000000 @
1@000@000@000
It defaults to a seperator of ",", but will take other with the second parameter (not needed).
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Here's another solution which lets you decide the splitting frequency and char.

Code: Select all

proc splitNumber {number {every 3} {by ,}} {
	while {[regsub ^(\[-+\]?\\d+)(\\d\{$every\}) $number \\1$by\\2 number]} {}
	set number
}
usage examples:

Code: Select all

splitNumber 10000
returns: 10,000

splitNumber 10000 4 .
returns: 1.0000
probably not very useful for most of you out there anyway :P
Locked