Version 3 of Narrow formatting

Updated 2004-02-11 09:16:31

if 0 {Richard Suchenwirth 2004-02-11 - Here's a routine for formatting non-negative integers (e.g. file sizes), so that the resulting field width does not exceed 5 characters, of which max. three significant digits, and K(ilo = 1024), M(ega = 1024^2) etc. modifiers are used for scaling. This may be useful on small displays.}

 proc fixform n {
    if {wide($n) < 1000} {return $n}
    foreach unit {K M G T P E} {
        set n [expr {$n/1024.}]
        if {$n < 1000} {
            set n [string range $n 0 3]
            regexp {(.+)\.$} $n -> n
            return $n$unit
        }
    }
    return Inf ;# :)
 }
 % fixform 1
 1
 % fixform 10
 10
 % fixform 100
 100
 % fixform 1000
 0.97K
 % fixform 10000
 9.76K
 % fixform 100000
 97.6K
 % fixform 1000000
 976K
 % fixform 10000000
 9.53M
 % fixform 100000000
 95.3M
 % fixform 1000000000
 953M

In the Tcl chatroom I was informed that according to http://physics.nist.gov/cuu/Units/binary.html , the units based on powers of 2 should be abbreviated

 Ki Mi Gi Ti Pi Ei

but I haven't seen that used yet... change if you wish.

DKF: Some standards are just destined to be ignored...