I just took your code as a starting point and modified it a bit. It would appear that just checking whether a decimal exists or not is sufficient. Take out that if logic if you want all data to be formatted like $x.yy.
#!/usr/bin/perl -w use strict; foreach my $in qw(-10000000 1000.000 10.000 9.999 10 10.1 0 12345 5.) { print "in = $in \tformatted = ",f_currency($in),"\n"; } sub f_currency { my $num = shift; my $rounded = $num; if ($num =~ m|\.|) { $rounded = sprintf("%.2f", $num); #rounds to 2 dec places } #add commas, all the "work" happens in the while condition while ($rounded =~ s/^(-?\d+)(\d\d\d)/$1,$2/){}; return ('$'.$rounded); } __END__ prints: in = -10000000 formatted = $-10,000,000 in = 1000.000 formatted = $1,000.00 in = 10.000 formatted = $10.00 in = 9.999 formatted = $10.00 in = 10 formatted = $10 in = 10.1 formatted = $10.10 in = 0 formatted = $0 in = 12345 formatted = $12,345 in = 5. formatted = $5.00

In reply to Re: Currency Format Issue by Marshall
in thread Currency Format Issue by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.