CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Adding a money character and justifying numbers



Dec 12, 2000, 10:19 AM

Post #1 of 2 (939 views)
Adding a money character and justifying numbers Can't Post

Hello !!

The next function adds a comma for indicating thousands and millions. What I need is to add a money character like "DM","$" or "" to the function. Where should I add it? I also need to justify numbers like this :
$ 6,256.30
$ 36,758.27
So that the money character doesn't moves. Just as is done in some electronic worksheets like Excel for example.
One last thing, this function doesn't works when I try to commify numbers which come from a table and already have a decimal point. What can I do?

Thanks a lot for any help!

sub commify {
my $input = shift;
$input = reverse $input;
$input =~ s<(\d\d\d)(?=\d)(?!\d*\.)><$1,>g;
return reverse $input;


Jan 22, 2001, 10:06 PM

Post #2 of 2 (915 views)
Re: Adding a money character and justifying numbers [In reply to] Can't Post

Displaying the dollar sign is easy just add it to the front of the string like this $string = \$ . $string;

In order to figure out where to put the $ sign and how many whitespaces to add, you need to find out which string has the greatest length. I'd suggest putting them in an array. If the difference in size between the smallest and the largest is two characters you'd need to do this.

$string = "\$ " . $string;

A foreach loop thru the array should be able to do everything for you. Go to the end of each string and move forward two places, then insert a decimal, move forward three (if it's that large) and insert a comma, etc. etc.

It can all be done on the basis of the size of the string.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives