how sub can have 2 decimal numbers only?

Discussion in 'Perl Misc' started by xman, May 23, 2005.

  1. xman

    xman Guest

    my $usdTotal = $Total / 6.2;

    when Perl calculate it I hzave result like $usdTotal = 120.2435445466

    but I need 120.24 only (2 digits after dot..)

    how I can do it?
     
    xman, May 23, 2005
    #1
    1. Advertising

  2. "xman" <> wrote in news:d6tca3$ce4$-com.hr:

    > my $usdTotal = $Total / 6.2;
    >
    > when Perl calculate it I hzave result like $usdTotal = 120.2435445466
    >
    > but I need 120.24 only (2 digits after dot..)
    >
    > how I can do it?


    perldoc -f sprintf

    Please consult the documentation first.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, May 23, 2005
    #2
    1. Advertising

  3. my $usdTotal = sprintf "%.2f",$Total/6.2;

    or

    my $usdTotal = int($Total/6.2*100)/100
     
    Mike Deskevich, May 23, 2005
    #3
  4. xman

    xman Guest

    thanks!

    "Mike Deskevich" <> wrote in message
    news:...
    > my $usdTotal = sprintf "%.2f",$Total/6.2;
    >
    > or
    >
    > my $usdTotal = int($Total/6.2*100)/100
    >
     
    xman, May 23, 2005
    #4
  5. xman <> wrote:

    > when Perl calculate it I hzave result like $usdTotal = 120.2435445466
    >
    > but I need 120.24 only (2 digits after dot..)
    >
    > how I can do it?



    perldoc -q round

    Does Perl have a round() function? What about ceil() and floor()?
    Trig functions?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, May 23, 2005
    #5
  6. also, i forgot to take care of any rounding. ignoring any precision
    errors, i should have said

    $usdTotal=int(($Total/6.2*100)+.5)/100;

    but you are correct about the precision errors. i forgot about that.
    i always do my conversions with sprintf, so i never have to worry about
    that.
     
    Mike Deskevich, May 24, 2005
    #6
    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. loga123
    Replies:
    2
    Views:
    350
    loga123
    May 20, 2006
  2. Ven
    Replies:
    3
    Views:
    1,367
  3. Gilbert Fine
    Replies:
    8
    Views:
    932
    Zentrader
    Aug 1, 2007
  4. Ben
    Replies:
    2
    Views:
    955
  5. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,168
    Lawrence D'Oliveiro
    May 20, 2011
Loading...

Share This Page