Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / Perl / Getting Started / October 2009



Tip: Looking for answers? Try searching our database.

printf with currency symbols

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bryan R Harris - 26 Oct 2009 16:57 GMT
Is there a good way to do printf's with currency symbols?

I've tried this:

 printf "Total: \$%10.2f\n", $total;

But it puts the dollar sign way out front (ugly).  I want it to look like:

 Total:    $24.15

Is there a way to do this without getting all messy like this?

 printf "Total:%10s\n", "\$".sprintf(%.2f,$total);

- Bryan

Signature

To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Robert Citek - 26 Oct 2009 17:27 GMT
Is this what you are looking for:

$ perl -e '$total = 24.15 ; printf "Total: \$%.2f\n", $total; '

Regards,
- Robert

> Is there a good way to do printf's with currency symbols?
>
[quoted text clipped - 9 lines]
>
>  printf "Total:%10s\n", "\$".sprintf(%.2f,$total);

Signature

To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Jim Gibson - 26 Oct 2009 17:29 GMT
> Is there a good way to do printf's with currency symbols?
>
[quoted text clipped - 5 lines]
>
>   Total:    $24.15

You can add a minus sign to the format descriptor to left-justify the field:

%-10.2f

However, if you do this and print more than one line, the decimal points may
not line up.

Signature

To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Robert Citek - 26 Oct 2009 17:43 GMT
I see.  You want the output to look something like this:

$ perl -e 'for(my $total = 24.15; $total <30000; $total *= 10) {
printf("Total:%10s\n", "\$" . sprintf("%.2f",$total)) ;} '
Total:    $24.15
Total:   $241.50
Total:  $2415.00
Total: $24150.00

Not sure if there is a better way.  My guess is that there is probably
some module to convert float to currency and then print it as a
string.  But a quick Google didn't turn up anything.

Good luck and let us know how things go.

Regards,
- Robert

> Is there a way to do this without getting all messy like this?
>
>  printf "Total:%10s\n", "\$".sprintf(%.2f,$total);

Signature

To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Shawn H Corey - 26 Oct 2009 19:34 GMT
> Not sure if there is a better way.  My guess is that there is probably
> some module to convert float to currency and then print it as a
> string.  But a quick Google didn't turn up anything.

Here' why (extracted from `perldoc perllocale`):

  Category LC_MONETARY: Formatting of monetary amounts

      The C standard defines the "LC_MONETARY" category, but no
function that is affected by its contents.  (Those with experience of
standards committees will recognize that the working group decided to
punt on the issue.)  Consequently, Perl takes no notice of it.  If you
really want to use "LC_MONETARY", you can query its contents--see "The
localeconv function"--and use the information that it returns in your
application’s own formatting of currency amounts.  However, you may well
find that the information, voluminous and complex though it may be,
still does not quite meet your requirements: currency formatting is a
hard nut to crack.

Signature

Just my 0.00000002 million dollars worth,
 Shawn

Programming is as much about organization and communication
as it is about coding.

I like Perl; it's the only language where you can bless your
thingy.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Bryan R Harris - 26 Oct 2009 19:53 GMT
>> Not sure if there is a better way.  My guess is that there is probably
>> some module to convert float to currency and then print it as a
[quoted text clipped - 14 lines]
> still does not quite meet your requirements: currency formatting is a
> hard nut to crack.

That's what I needed to know -- thanks Shawn (and Jim and Robert).

- Bryan

Signature

Bryan Harris
Sr. Systems Engineer II
Huntsville Operations Analysis & System Performance
Missile Systems, Raytheon Company
bh@raytheon.com
256.542.4632

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.