 We don't bite newbies here... much PerlMonks

### How do I round a number?

 on Jul 25, 2000 at 23:48 UTC Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (math)

How do I round a number?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I round a number?
by ryanus (Sexton) on Jul 06, 2002 at 22:15 UTC

I would use Math::Round. It is easy to use. For example:

```use Math::Round;

print nearest(.01, 1.555);
prints '1.56'.

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.
This concept presented above can be expanded to round to more (or less) percision than just "round number to nearest integer". i.e.

Round to nearest hundred (100):

```\$rounded=100*int(0.5+\$number/100);
Round to nearst tenth (0.1):
```\$rounded=0.1*int(0.5+\$number/0.1);
etc...
\$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 half-way case
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);
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;
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 ? int(\$_ + .5) : -int(-\$_ + .5)
}
Re: How do I round a number?
by powerman (Friar) on Apr 23, 2002 at 11:44 UTC
Here shown all round-like 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
```
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.

Caution: Contents may have been coded under pressure.
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.

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

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.
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... :)
Re: How do I round a number?
by I0 (Priest) on Jan 03, 2001 at 16:08 UTC
perldoc -q "round function"

Originally posted as a Categorized Answer.

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

Originally posted as a Categorized Answer.

Re: How do I round a number?
by Anonymous Monk on May 22, 2003 at 00:55 UTC
how about a round function that let me round percentages so that they total up to 100%? anything that rounds down won't do that ...

Originally posted as a Categorized Answer.

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://24335]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2022-05-20 16:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Do you prefer to work remotely?

Results (75 votes). Check out past polls.

Notices?