use strict;

# Beautify a number.  Turn 3576281 into $3.6M, 143,999 into $144K,
#  and 1153 into $1,153.  Might want to think of generalizing it for
#  multi-currencies at some point.
# Doesn't deal with negative numbers.
#
sub beautify
{
    my $in = shift;
    my ($l, $d);

    if    ($in >= 1000000000) { $l = 'B'; $d = 100000000.0;  }
    elsif ($in >= 1000000)    { $l = 'M'; $d = 100000.0; }
    elsif ($in >= 1000)       { $l = 'K'; $d = 100.0; }

    else { return "\$" . &commify(sprintf('%0.2f', $in)); }

    $in = (int(($in / $d) + 0.5)) / 10.0;
    return sprintf "\$%.1f$l", $in;
}

# stolen from perlfaq5.  The author (not me) is a show-off
#
sub commify
{
    local $_  = shift;
    1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
    return $_;
}

1;
