Version 12 of commas added to numbers

Updated 2010-11-16 23:20:08 by jbr
What
commas added to numbers
Where
From the contact
Description
Tiny Tcl regsub comma to add commas to numbers, taking leading white space, - or +, and decimal points into account.
Updated
?
Contact
mailto:[email protected] (John Allen)

In Bag of algorithms there's a one-liner alternative by Peter Spjuth (in the Tcl chatroom, 2004-10-05) using modern regexp features:

 proc commify number {regsub -all {\d(?=(\d{3})+($|\.))} $number {\0,}}

e.g.:

  % commify 1000000.00
  1,000,000.00

Comment: commify only works for numbers with less than 3 digits after decimal point e.g.

 () 4 % commify 1234567.123456
 1,234,567.123,456

AMG: I added some code to SYStems's page that puts _'s in numbers in the same place commas would go. There's both a [for] version and a [regsub] version. It's not possible (I don't think) to make a one-liner [regsub] that correctly places commas after the decimal point since such a feature would require "look-behind" patterns, so I reverse the string, [regsub], then reverse again.


KPV The following is a tcl translation of a perl code snippet to add commas to a number. It's not quite a one-liner but pretty close.

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

If you want groupings other than 3 try:

 proc commify { num {sep ,} {groupSize 3}} {
    while {[regsub "^(\[-+]?\\d+)(\\d{$groupSize})" $num "\\1$sep\\2" num]} {}
    return $num
 }

WJP See Delimiting Numbers for a procedure that can handle groups of size other than three (such as the groups of size four used in some Asian languages).


jbr The one liner is the combination of the original code above and the explicit selection of the trailing decimals:

 proc commify number {regsub -all \\d(?=(\\d{3})+([regexp -inline {\.\d*$} $number]$)) $number {\0,}}