[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

I'm very new to Perl Is there an easier way to do this? Format Current?

Posted on 2006-06-02
7
Medium Priority
?
244 Views
Last Modified: 2006-11-18
sub CurrencyFormatted
{
   my $n = shift;
   my $minus = $n < 0 ? '-' : '';
   $n = abs($n);
   $n = int(($n + .005) * 100) / 100;
   $n .= '.00' unless $n =~ /\./;
   $n .= '0' if substr($n,(length($n) - 2),1) == '.';
   chop $n if $n =~ /\.\d\d0$/;
   return "$minus$n";
}
0
Comment
Question by:cedent
  • 5
7 Comments
 
LVL 8

Expert Comment

by:Perl_Diver
ID: 16819613
maybe:


 sub CurrencyFormatted
{
   my $n = shift;
   my $n = sprintf('%.2f',$n);
   return "$n";
}
0
 
LVL 8

Expert Comment

by:Perl_Diver
ID: 16819641
you can reduce that if you wanted to:

sub CurrencyFormatted
{
   return(sprintf('%.2f',shift));
}
0
 
LVL 8

Expert Comment

by:Perl_Diver
ID: 16819666
all it really appears to do is add two decimal places (the original code you posted)  or reduce to two decimal places, it's not really a currency format with commas or other symbols for the left side of the decimal, but maybe thats not wanted or necessary.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 8

Accepted Solution

by:
Perl_Diver earned 800 total points
ID: 16820319
oops, my first suggestion needed better proof reading, should really be:

 sub CurrencyFormatted
{
   my $n = shift;
   $n = sprintf('%.2f',$n);
   return $n;
}

but as I showed in the second example you don't need any temp variables at all to do this.
0
 
LVL 17

Assisted Solution

by:mjcoyne
mjcoyne earned 600 total points
ID: 16823219
What about the Number::Format module (see http://search.cpan.org/dist/Number-Format/Format.pm)?  This will do all the thousands seperators, deal with negative numbers, set what ever decimal precision you want, etc.:

  use Number::Format;
  my $fnum;

  my $usd = new Number::Format(-thousands_sep   => ',',
                              -decimal_point   => '.',
                              -int_curr_symbol => 'USD');

  if ($num >- 0) {
    $fnum = $usd->format_number($num);
  } else {
    $fnum = $usd->format_negative($num, "-x");
  }
0
 
LVL 8

Expert Comment

by:Perl_Diver
ID: 16824347
heh.... so many perl modules, I was going to suggest:

http://search.cpan.org/~jpeacock/Math-Currency-0.44/lib/Math/Currency.pm

but the OP asked for a simpler way.
0
 
LVL 2

Assisted Solution

by:alexsergeyev
alexsergeyev earned 600 total points
ID: 16830784
just a small extension to Perl_Diver ; if you would need different styles for thousands and choose style depending on situation you might use my test code as sample:

#!/usr/bin/perl -w
use strict;

sub CurrencyFormatted ($;$$;$$$) {
  my ($n, $showcents, $thousandsep) = @_;
  $n = sprintf($showcents ? "%.2f" : "%d", $n);
  if ($thousandsep) {
      my $pos = length($n) - ($showcents ? 6 : 3);
      while ($pos > 0) {
         substr($n, $pos, 0, $thousandsep);
         $pos -= 3;
      }
  }
  return $n;
}

printf "%s\n"x4,
    CurrencyFormatted(1234.56),
    CurrencyFormatted(1234.56, 0, ' '),
    CurrencyFormatted(1234.56, 1),
    CurrencyFormatted(1234.56, 1, ' ');

printf "html-like:  %s\n", CurrencyFormatted(123456789.12, 1, '&nbsp;');


You also may optionally return "\$$n" if you need dollar sign.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question