Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi, I'm looking for a perl scrip 4

Status
Not open for further replies.

Leland123

Programmer
Aug 15, 2001
402
US
Hi,

I'm looking for a perl script that would convert a number into a dollar amount like:

$the_number = "1234567.89";

perl would build $the_number into $the_dollars
so that:

print $the_dollars;


$1,234,567.89


LelandJ

Leland F. Jackson, CPA
Software - Master (TM)
Nothing Runs Like the Fox
 
Leland,
I've always found the easiest way to do this is via the Number::Format module. Here is a quick example:
Code:
use Number::Format;
my $usd = new Number::Format(-int_curr_symbol => '$');
$the_number = "1234567.89";
print $usd->format_price($the_number), "\n";
Hope that helps.
 
Hi usige,

Thanks for the info. I knew it would be a waste of time to write this, because there was bound to be a script already written, rewritten, and well tested.

LelandJ

Leland F. Jackson, CPA
Software - Master (TM)
Nothing Runs Like the Fox
 
or this way with no module...

print "\$" . sprintf ("%.2f", 1234567.89) . "\n";

regards
Duncan
 
Hi usige & Duncan,

Both solutions work well. I haven't decided which way to go.

The Number::Format modules is nice, but it creates one more dependency in an app that I may distribute. I try to keep my perl application dependencies to a minimum. Also, Number::Format places a space between the dollar sign and the number. When phrasing a postgres shopping cart database into html for display of products, including product sale prices, the html table often centers the dollar sign on the line above the line in which the sales price appears. This looks a little weired.

The advantages of the Number::Format module is it provide more functionality than formatting the number using sprintf. Sprintf doesn't place any commas in the output, which makes the number a little harder to read, but I can place the dollar sign next to the beginning number and avoid the problem of the html table wrapping the amount on the line under the line in which the dollar sign appears. The sprintf is simpler to use and does not require any special module download from CPAN, which eliminate a dependency of my perl application.

At this point I'm leaning towards going with the Number::Format modules as I'm sure it will come in handy later.

Thanks for the help.

LelandJ

Leland F. Jackson, CPA
Software - Master (TM)
Nothing Runs Like the Fox
 
Thanks, Paul. I started down the regex path but was too lazy to play with fixing the problem that the double reversing takes care of. A+ for you.
 
Neat Paul. Annoyingly neat in fact :)

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top