How do I get trim or rounding on a float number ?

2002-02-08 Thread FLAHERTY, JIM-CONT

$ aver = $total_hours/$total_jobs
 
 
some times comes up with   24.97  . I would like to round or trim to
24.99 for example .  any Ideas  ??
 
 
thanks 
Jim F
 



Re: How do I get trim or rounding on a float number ?

2002-02-08 Thread Brett W. McCoy

On Fri, 8 Feb 2002, FLAHERTY, JIM-CONT wrote:

> $ aver = $total_hours/$total_jobs
>
>
> some times comes up with   24.97  . I would like to round or trim to
> 24.99 for example .  any Ideas  ??

my $aver = sprintf("%.2f", $total_hours/$total_jobs);

perldoc -f sprintf

-- Brett
  http://www.chapelperilous.net/

Forecast, n.:
A prediction of the future, based on the past, for
which the forecaster demands payment in the present.


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




Re: How do I get trim or rounding on a float number ?

2002-02-08 Thread Jeff 'japhy' Pinyan

On Feb 8, FLAHERTY, JIM-CONT said:

>some times comes up with   24.97  . 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 **
 what does y/// stand for?   why, yansliterate of course.


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




RE: How do I get trim or rounding on a float number ?

2002-02-08 Thread John Edwards

Well, do you want to round or trim? The number you mention rounded to 2dp
would be 25.00. Trimmed to 2dp would be 24.99.

To round the number you could do this

$number = "24.97";
$rounded = sprintf "%.2f",$number;
print $rounded;

To trim you could do

$number = "24.97";
($rounded) = $number =~ /(\d*\.\d{2})/;
print $rounded;

John

-Original Message-
From: FLAHERTY, JIM-CONT [mailto:[EMAIL PROTECTED]]
Sent: 08 February 2002 14:48
To: Beginners (E-mail)
Subject: How do I get trim or rounding on a float number ?


$ aver = $total_hours/$total_jobs
 
 
some times comes up with   24.97  . I would like to round or trim to
24.99 for example .  any Ideas  ??
 
 
thanks 
Jim F
 


--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]




RE: How do I get trim or rounding on a float number ?

2002-02-08 Thread John Edwards

Ah. Reading this I realised that while the number you gave (24.97) 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.97  . 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 **
 what does y/// stand for?   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]




RE: How do I get trim or rounding on a float number ?

2002-02-08 Thread Jeff 'japhy' Pinyan

[Please do not top-post -- it makes the conversation difficult to follow]

On Feb 8, John Edwards said:

>>From: Jeff 'japhy' Pinyan [mailto:[EMAIL PROTECTED]]
>>
>>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;
>
>Ah. Reading this I realised that while the number you gave (24.97) 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;

That's the same method I use for my round() function, except I take it one
step further and add code that handles negative numbers (which should have
..5 subtracted, not added.

  int($number * 10**$n + ($number < 0 ? -.5 : .5)) / 10**$n;

Someone (either here or on comp.lang.perl.misc) pointed out I could write

  int($number * 10**$n + .5 * ($number <=> 0)) / 10**$n;

-- 
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 **
 what does y/// stand for?   why, yansliterate of course.


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