Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Formatting numbers to currency format

Reply
Thread Tools

Formatting numbers to currency format

 
 
Robert TV
Guest
Posts: n/a
 
      06-19-2004
I would like to know if Perl has a function to correctly format a string of
numbers into currency format. I have a small subroutine that takes a number
and adds a tax amount. Below is the base:

$amount = 100;
$tax = 0.05 # 5% tax rate
$finaltotal = $amount * $tax;
$finaltotal = sprintf("%.02f",$finaltotal);

Sometimes when then $amount is a complex number such as 1423 I will get
several decimal places, and sprintf rounds to two decimal places. This is
good. But when the amount like simple as seen above, there are no cedimal
places, therefor sprintf doesnt have anything to round. I am trying to make
it so all $finaltotals have two decimal places, even if they are .00 or .90
etc. Can anyone help? TIA!

Robert


 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      06-19-2004
Robert TV <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> I would like to know if Perl has a function to correctly format a string of
> numbers into currency format. I have a small subroutine that takes a number
> and adds a tax amount. Below is the base:
>
> $amount = 100;
> $tax = 0.05 # 5% tax rate


There's a semicolon missing after this statement. This is not the code
you ran.

Don't re-type code, copy and paste it.

> $finaltotal = $amount * $tax;


Shouldn't that be "$amount * ($tax + 1)"?

> $finaltotal = sprintf("%.02f",$finaltotal);
>
> Sometimes when then $amount is a complex number such as 1423 I will get
> several decimal places, and sprintf rounds to two decimal places. This is
> good. But when the amount like simple as seen above, there are no cedimal
> places, therefor sprintf doesnt have anything to round. I am trying to make
> it so all $finaltotals have two decimal places, even if they are .00 or .90
> etc. Can anyone help? TIA!


Are you saying, "sprintf '%.02f', ..." isn't showing trailing zeroes?
It should, and it does for me.

Show runnable code that demonstrates your problem.

Anno
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-22-2004
Robert TV wrote:
> I would like to know if Perl has a function to correctly format a
> string of numbers into currency format.


Why did you start a new thread on this topic, instead of following up
the replies at the thread you started a few days ago??

http://groups.google.com/groups?thre...0uni-berlin.de

( Are only morons asking questions nowadays? )

> $finaltotal = sprintf("%.02f",$finaltotal);


<snip>

> ... there are no cedimal places, therefor sprintf doesnt have
> anything to round.


Did you ever run any code that made you come to that conclusion? In
that case, please show us THAT code!

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
currency formatting in a MaskFormater CD1 Java 7 02-04-2011 07:49 PM
Converting string to currency/formatting in thousands.aspx (.NET) pebelund ASP .Net 1 10-23-2006 10:32 AM
Currency Formatting Vlad ASP .Net 3 05-26-2004 01:52 PM
Formatting Currency According to Locale Daniele Varrazzo Python 0 04-28-2004 04:46 PM
currency formatting for Euro ashish ASP .Net 0 04-27-2004 08:36 PM



Advertisments