Hi Monks!
I need to check currency values and format them, my question is what is the best way. I am working on this code, but if the values checked doesn't have decimal on them the code will not work, any suggestions? Thanks!
#!/usr/bin/perl -w
use strict;
my $dollar="100"; #test 1
#my $dollar="100.000"; #test 2
#my $dollar="10.000"; #test 3
my $amount= f_currency($dollar);
print "Testing=$amount\n";
sub f_currency {
my $num = shift @_;
my $number;
#make sure that the amount entering this sub has the decimal point fol
+lowed by two numbers, if not dont go there.
if($num=~/\d+\.\d{2}$/g) # This regular is not checking properly
{
$number = sprintf "%.2f", $num;
# Add one comma each time through the do-nothing loop
1 while $number =~ s/^(-?\d+)(\d\d\d)/$1,$2/;
# Put the dollar sign in the right place
$number =~ s/^(-?)/$1\$/;
return $number;
}else{ #if the number dotn have the expected format just add the $ sig
+n in front of it.
$num="\$".$num;
return $num;
}
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.