[illumos-Developer] Formatting human readable numbers

Ceri Davies ceri at submonkey.net
Wed Jan 12 03:08:44 PST 2011


On Tue, Jan 11, 2011 at 11:22:04PM -0600, Jason King wrote:
> Currently, in swap(1m), du(1), df(1), and ls(1) they all support a -h
> flag for printing values in human readable formats (i.e. 1.5k, 25M,
> etc.). ?du and df also support a -H flag to use powers of 10 instead
> of 2, but the same concept.  The source for all of these all use a
> function number_to_scaled_string to generate the output. ?It looks
> very much like it was almost copy/pasted verbatim between all of them.
> ?It seems silly to have 4 implementations of the same function
> floating around.
> I think this is a fairly good (and simple) candidate to put into a
> central library. ?The questions are: which library would be most
> appropriate, should this be classified as (private, etc.), and should
> the prototype change any.  I'd like some guidance on these.  I'm not
> aware if there are equivalent functions in one of the BSDs or Linux,
> though if anyone is aware, that might be a good place to start.

FreeBSD and NetBSD have humanize_number() and expand_number() for this
in libutil.  See
http://www.freebsd.org/cgi/man.cgi?query=humanize_number

Ceri
-- 
Haffely, Gaffely, Gaffely, Gonward.



More information about the Developer mailing list