eXtropia: the open web technology company
Technology | Support | Tutorials | Development | About Us | Users | Contact Us
Development resources
 ::   WebWare 2.1 (Perl)
 ::   WebWare 2.0 (Java)
 ::   Cool hacks
radical hacks
WebStore Formatting Currencies  
sent in the following note about formatting currencies....

Problem: "In our country, prices are formatted like this..

        $100 = 52.000.000 TL
So how should I format the prices as shown above. Sprintf and .2f is not for us.

Answer: What you have is a variant on the problem "Putting Commas in Numbers", which is covered nicely by the Perl Cookbook, recipe 2.17:

sub commify {
  my $text = reverse $_[0];
  $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
  return scalar reverse $text;
}
print commify("52000000 TL");

This prints

52,000,000 TL

As you might imagine, you can easily alter the 2nd line of the function to insert decimal points:

  $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1./g;