Rounding numbers with sprintf

R

Robert TV

Hi,

A portion of my script uses 'sprintf' to round some of my taxed currency
vaiables to 2 decimal places. I am having a problem when the rounding does
not produce 2 full decimal places. My formula EG:

$price = 12.99; #product price of $12.99
$taxrate = 7.0; #tax rate of 7%
$taxtotal = $price / 100; #divides price by 100
$taxtotal = $taxtotal * $taxrate; #multiply divided price times tax rate
$taxtotal = sprintf("%.02f",$taxtotal); #round the tax total - 2 decimal
places
$finalprice = $price + $taxtotal; #add the tax total to the original
price
print "$finalprice";

This method produces: 13.9
I would like it to produce: 13.90

Most of the prices this script crunches produces 2 decimal place final
totals, it's only when the tax total round evenly to 10/100'ths. How can I
modify this script to add a "0" (zero) to the end of any total that does not
have 2 decimal places? Also, if the above method for calculating the taxed
total is inefficient, I welcome alternative suggestions. TIA!

Robert
 
S

Sam Holden

Hi,

A portion of my script uses 'sprintf' to round some of my taxed currency
vaiables to 2 decimal places. I am having a problem when the rounding does
not produce 2 full decimal places. My formula EG:

$price = 12.99; #product price of $12.99
$taxrate = 7.0; #tax rate of 7%
$taxtotal = $price / 100; #divides price by 100
$taxtotal = $taxtotal * $taxrate; #multiply divided price times tax rate
$taxtotal = sprintf("%.02f",$taxtotal); #round the tax total - 2 decimal
places
$finalprice = $price + $taxtotal; #add the tax total to the original
price
print "$finalprice";

Don't quote things which don't need to be quoted.
This method produces: 13.9
I would like it to produce: 13.90

Most of the prices this script crunches produces 2 decimal place final
totals, it's only when the tax total round evenly to 10/100'ths. How can I
modify this script to add a "0" (zero) to the end of any total that does not
have 2 decimal places? Also, if the above method for calculating the taxed
total is inefficient, I welcome alternative suggestions. TIA!


See "perldoc -q round" for details on why using sprintf for rounding
financial values is usually not a wise choice (chances are the tax laws
don't use the IEEE floating point rounding conventions used by perl)

The trailing 0 is being lost when producing the numeric result of
$price + $taxtotal.

If you want a trailing zero you need a string, not a number so replace:

print "$finalprice";

with:

printf "%.2f", $finalprice
 
P

Paul Lalli

Hi,

A portion of my script uses 'sprintf' to round some of my taxed currency
vaiables to 2 decimal places. I am having a problem when the rounding does
not produce 2 full decimal places. My formula EG:

$price = 12.99; #product price of $12.99
$taxrate = 7.0; #tax rate of 7%
$taxtotal = $price / 100; #divides price by 100
$taxtotal = $taxtotal * $taxrate; #multiply divided price times tax rate
$taxtotal = sprintf("%.02f",$taxtotal); #round the tax total - 2 decimal
places
$finalprice = $price + $taxtotal; #add the tax total to the original
price
print "$finalprice";

This method produces: 13.9
I would like it to produce: 13.90

You created the proper format with $taxtotal, but then used it as a number
again and printed out $finalprice. You need to create the proper format
on $finalprice if that's what you're printing.

$taxtotal = $taxtotal * $taxrate;
$finalprice = $price + $taxtotal;
printf ("%.02f\n", $finalprice);


By the way, the line
print "$finalprice";
is uselessly using double quotes. Read the perlfaq for "What's wrong with
always quoting".

Paul Lalli
 
G

Gunnar Hjalmarsson

Robert said:
A portion of my script uses 'sprintf' to round some of my taxed
currency vaiables to 2 decimal places. I am having a problem when
the rounding does not produce 2 full decimal places. My formula EG:

$price = 12.99; #product price of $12.99
$taxrate = 7.0; #tax rate of 7%

That assigns a number to $taxrate. Assign a string if you want to
preserve the decimal point:

$taxrate = '7.0';
$taxtotal = $price / 100; #divides price by 100
$taxtotal = $taxtotal * $taxrate; #multiply divided price times
tax rate
$taxtotal = sprintf("%.02f",$taxtotal); #round the tax total - 2
decimal places

Why not just:

$taxtotal = sprintf '%.02f', $price * $taxrate / 100;
$finalprice = $price + $taxtotal; #add the tax total to the
original price

An addition of two numbers returns a number. If you want to assign a
string to $finalprice, you need to say so:

$finalprice = sprintf '%.02f', $price + $taxtotal;
How can I modify this script to add a "0" (zero) to the end of any
total that does not have 2 decimal places?

You can't. You need to convert those numbers to strings before
printing them.
 

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,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top