Rounding numbers with sprintf

Discussion in 'Perl Misc' started by Robert TV, Jun 12, 2004.

  1. Robert TV

    Robert TV Guest

    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
     
    Robert TV, Jun 12, 2004
    #1
    1. Advertising

  2. Robert TV

    Sam Holden Guest

    On Sat, 12 Jun 2004 02:17:10 GMT, 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";


    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
     
    Sam Holden, Jun 12, 2004
    #2
    1. Advertising

  3. Robert TV

    Paul Lalli Guest

    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
     
    Paul Lalli, Jun 12, 2004
    #3
  4. 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
     
    Gunnar Hjalmarsson, Jun 12, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C

    Rounding Numbers

    C, Aug 25, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    511
  2. asutic

    rounding of 4 unsigned char numbers

    asutic, Aug 1, 2008, in forum: C Programming
    Replies:
    2
    Views:
    274
    Herbert Kleebauer
    Aug 1, 2008
  3. Mike Flannigan

    Matching FP Numbers and Using Sprintf

    Mike Flannigan, Aug 23, 2003, in forum: Perl Misc
    Replies:
    7
    Views:
    106
    J├╝rgen Exner
    Aug 25, 2003
  4. Blnukem
    Replies:
    9
    Views:
    185
  5. Replies:
    5
    Views:
    133
    Ilya Zakharevich
    Jan 24, 2008
Loading...

Share This Page