Welcome, guest | Sign In | My Account | Store | Cart

This piece of code inserts a thousandth separator into a decimal number, i.e. every 3 digits the separator character is inserted. courtesy Jeff Hobbs's bag of utilities.

Tcl, 13 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# commify --
#   puts commas into a decimal number
# Arguments:
#   num		number in acceptable decimal format
#   sep		separator char (defaults to English format ",")
# Returns:
#   number with commas in the appropriate place
#

proc commify {num {sep ,}} {
    while {[regsub {^([-+]?\d+)(\d\d\d)} $num "\\1$sep\\2" num]} {}
    return $num
}
Created by andreas kupries on Thu, 22 Aug 2002 (MIT)
Tcl recipes (162)
andreas kupries's recipes (20)

Required Modules

  • (none specified)

Other Information and Tasks