Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Rounding numbers with sprintf

Reply
Thread Tools

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


 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
Gunnar Hjalmarsson
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.

--
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
rounding of 4 unsigned char numbers asutic C Programming 2 08-01-2008 07:20 PM
sprintf rounding with FreeBSD and perl 5.8.x cherbst@gmail.com Perl Misc 5 01-24-2008 10:10 PM
Rounding up anything past the decimal point sprintf Blnukem Perl Misc 9 09-04-2003 10:39 PM
Rounding Numbers C ASP .Net 2 08-25-2003 03:24 PM
Matching FP Numbers and Using Sprintf Mike Flannigan Perl Misc 7 08-25-2003 12:14 PM



Advertisments