Ah. Reading this I realised that while the number you gave (24.999997) will
round to 25.00 using sprintf, not all number will round as expected using
that methos. Instead you can use this (which does appear to work).

$number = "12.345";             # Round this number
$n = 2;                         # to this many places
$rounded = int($number * (10 ** $n) + .5) / (10 ** $n);
print $rounded;

HTH

John

-----Original Message-----
From: Jeff 'japhy' Pinyan [mailto:[EMAIL PROTECTED]]
Sent: 08 February 2002 15:01
To: FLAHERTY, JIM-CONT
Cc: Beginners (E-mail)
Subject: Re: How do I get trim or rounding on a float number ?


On Feb 8, FLAHERTY, JIM-CONT said:

>some times comes up with   24.999997  . I would like to round or trim to
>24.99 for example .  any Ideas  ??

  perldoc -q round

will tell you about whether or not Perl has a rounding function.

If you're REALLY worried about whether 12.345 rounds to 12.34 or 12.35,
then you should use a specific rounding function, but if not, you can use
sprintf():

  $num = sprintf "%.02f", $raw;

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--------------------------Confidentiality--------------------------.
This E-mail is confidential.  It should not be read, copied, disclosed or
used by any person other than the intended recipient.  Unauthorised use,
disclosure or copying by whatever medium is strictly prohibited and may be
unlawful.  If you have received this E-mail in error please contact the
sender immediately and delete the E-mail from your system.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to