Formatting numbers to currency format

R

Robert TV

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
 
A

Anno Siegel

Robert TV said:
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
 
G

Gunnar Hjalmarsson

Robert said:
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/[email protected]

( Are only morons asking questions nowadays? :( )
$finaltotal = sprintf("%.02f",$finaltotal);

... 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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top