Velocity Reviews > Perl > Rounding numbers with sprintf

# Rounding numbers with sprintf

Robert TV
Guest
Posts: n/a

 06-12-2004
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

Sam Holden
Guest
Posts: n/a

 06-12-2004
On Sat, 12 Jun 2004 02:17:10 GMT, Robert TV <(E-Mail Removed)> wrote:
> 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

--
Sam Holden

Paul Lalli
Guest
Posts: n/a

 06-12-2004
On Sat, 12 Jun 2004, Robert TV wrote:

> 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

Guest
Posts: n/a

 06-12-2004
Robert TV wrote:
> 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.

--
Email: http://www.gunnar.cc/cgi-bin/contact.pl