It has been a while since there has been a good round of golf, so I decided to post one. I couldn't come up with a nice interesting math brain-teaser, so I'll post this one.
The Rules
- Write a sub that takes a number as an argument and returns it in the standard monetary format -- with comma's and to two decimal places
Expected Input-Output
- Input: 3
- Output: 3.00
- Input: 1000
- Output: 1,000.00
- Input: 12345678
- Output: 12,345,678.00
- Input: 4444444.11
- Output: 4,444,444.11
- Input: 98765.343
- Output: 98,765.34
- Input: 111.229
- Output: 111.23
My Swing (83 chars)
sub a
{
$_=pop;($_=reverse sprintf"%.2f",$_)=~s/(\d\d\d(?=\d))/$1,/g,return re
+verse split''
}
|
Enoch
(update: Changed "integer" to "number". Integers don't have decimals. Thanks to xylus for pointing out my err.
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, 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, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|