Re: How do I round a number?⭐
by ryanus (Sexton) on Jul 06, 2002 at 22:15 UTC

use Math::Round;
print nearest(.01, 1.555);
prints '1.56'.
 [reply] [d/l] 
Re: How do I round a number?⭐
by fundflow (Chaplain) on Jul 26, 2000 at 00:22 UTC

The guy asked to round a number, the simplest way
is (my highschool teacher would be proud now..):
$rounded = int ( $orig + 0.5 )
This approach floors any decimal portion less than 0.5, and rounds up (in value) any decimal portion greater than .5. That means the following:
1.1 rounds to 1.0.
1.5 rounds to 2.0.
1.1 rounds to 1.0.
1.5 rounds to 1.0.
 [reply] [d/l] [select] 

$rounded=100*int(0.5+$number/100);
Round to nearst tenth (0.1):
$rounded=0.1*int(0.5+$number/0.1);
etc...
 [reply] [d/l] [select] 

$rounded = int ( $orig + 0.5 ) assumes $rounded is positive.
More generally: use POSIX; $rounded = floor ( $orig + 0.5 )
or $rounded = sprintf( "%.f" , $orig ) to properly handle the halfway case
See also `perldoc q round`
 [reply] [d/l] [select] 
Re: How do I round a number?⭐
by buckaduck (Chaplain) on Apr 23, 2001 at 12:32 UTC

For scientific applications requiring the use of significant
figures ("sig figs"), I strongly recommend the
Math::SigFigs module. Unfortunately, the
CPAN testers still haven't cleared it for Windows
clients, though...
use Math::SigFigs;
print FormatSigFigs($number, $digits);
 [reply] [d/l] 
Re: How do I round a number?⭐
by jlistf (Monk) on Jul 26, 2000 at 00:15 UTC

POSIX probably has an appropriate routine that'll do just that. You could also try using sprintf with the appropriate %0.2f (or whatever precision you're looking for). finally (TMTOWTDI), you could use the int keyword to truncate it, which might be more effective. for example, to generate dice rolls:
int( rand 6 ) +1;
 [reply] [d/l] 
Re: How do I round a number?⭐
by japhy (Canon) on Jul 26, 2000 at 05:37 UTC

Different rounding schema:
# 1.1 => 1; 1.9 => 1; 1.1 => 2; 1.9 => 2
$rounded = POSIX::floor($value);
# 1.1 => 2; 1.9 => 2; 1.1 => 1; 1.9 => 1
$rounded = POSIX::ceil($value);
# 1.1 => 1; 1.9 => 2; 1.1 => 1; 1.9 => 2
$rounded = round($value);
sub round {
$_[0] > 0 ? int($_[0] + .5) : int($_[0] + .5)
}
 [reply] [d/l] 
Re: How do I round a number?⭐
by powerman (Friar) on Apr 23, 2002 at 11:44 UTC

Here shown all roundlike functions which exists in perl:
#!/usr/bin/perl
use POSIX;
@a=(3.3, 3.5, 3.7, 3.3, 3.5, 3.7, 3.45);
print "number\tint\tprintf\tfloor\tceil\n";
printf "%.2f\t%.1f\t%.1f\t%.2f\t%.2f\n",
$_,
int,
$_,
floor($_),
ceil($_)
foreach (@a);
This code produce this output:
number int printf floor ceil
3.30 3.0 3.3 3.00 4.00
3.50 3.0 3.5 3.00 4.00
3.70 3.0 3.7 3.00 4.00
3.30 3.0 3.3 4.00 3.00
3.50 3.0 3.5 4.00 3.00
3.70 3.0 3.7 4.00 3.00
3.45 3.0 3.5 3.00 4.00
 [reply] [d/l] 
Re: How do I round a number?⭐
by 5mi11er (Deacon) on Apr 27, 2005 at 16:54 UTC

I was looking for a ceil(x,y) function similar to what exists in excel, where x is the thing to round, and y is "significance" according to Excel v9 (Office 2000), I prefer to think of it as "interval".
But, I was also intrigued by several of the other answers given (found via supersearch), and then in a fit of playing around, I created several variations below.
My personal restrictions were to use math operations, and not rely on other modules. This eliminated the printf and POSIX answers.
 [reply] [d/l] 

 [reply] 

Wow, that's quite a scratchpad, thanks.
So, I hadn't thought much about negative numbers. Is it mathematically correct for the ceiling function to go to the next more negative number (to the left on a number line) or to truncate (ie int) a negative number (move toward the right on a number line)?
I think the rounding functions for negative numbers are correct (don't need adjusting), you're simply moving toward the nearest whole number...
Scott
Update:
As further discussed below, the code did originally have issues, the code in the answer above has now been replaced by working code.
 [reply] 


Re: How do I round a number?
by Mago (Parson) on Jul 09, 2003 at 19:25 UTC

If you are using integers, and want to use Math::BigInt:
Math::BigInt  Arbitrary size integer math package
DESCRIPTION
All operators (inlcuding basic math operations) are overloaded if you declare your big integers as
$i = new Math::BigInt '123_456_789_123_456_789';
(snip)
METHODS
round
$x>round($A,$P,$round_mode); # round to accuracy or precision using
+ mode $r
 [reply] [d/l] [select] 
Re: How do I round a number?
by Nimster (Sexton) on Jul 26, 2000 at 09:46 UTC

Use integer;
$thevalue*=1;
Seems the simplest, IMHO.
It rounds everything down, btw  so it acts kinda like 'div' in PASCAL. That's where it's useful. For rounding to the nearest, use any of the above.  [reply] [d/l] 

I think you mean int?(been a while since I used pascal).
Div was the divisor operation. The result being not a
rounded down number, but the divisor of the operation.
as in:
5 div 2 = 2
meaining 5/2 = 2 remainder 1 .
Div returns the 2, Mod returns the 1.
Admittedly this is irrelivant to the origonal question,
but... :)
 [reply] 
Re: How do I round a number?
by I0 (Priest) on Jan 03, 2001 at 16:08 UTC

 [reply] 
Re: How do I round a number?
by Anonymous Monk on Feb 16, 2004 at 08:59 UTC

 [reply] 
Re: How do I round a number?
by Anonymous Monk on May 22, 2003 at 00:55 UTC

 [reply] 