#!/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 followed 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 $ sign in front of it. $num="\$".$num; return $num; } }