Perl Monk, Perl Meditation PerlMonks

### How do I print/round a number to a given number of decimal places?

by vroom (His Eminence)
 on Jan 08, 2000 at 08:23 UTC Need Help??

vroom has asked for the wisdom of the Perl Monks concerning the following question: (data formatting)

How do I print/round a number to a given number of decimal places?

Originally posted as a Categorized Question.

• Comment on How do I print/round a number to a given number of decimal places?

Replies are listed 'Best First'.
Re: How do I print/round a number to a given number of decimal places?
by vroom (His Eminence) on Jan 11, 2000 at 01:22 UTC

Probably the easiest way is to use printf/sprintf:

```\$unrounded = 0.66666;

printf "\$unrounded rounded to 3 decimal places is %.3f\n", \$unrounded;

\$rounded = sprintf "%.2f", \$a;  # rounded to 2 decimal places (0.67)
Re: How do I print/round a number to a given number of decimal places?
by hossman (Prior) on May 30, 2003 at 06:39 UTC
Re: How do I print/round a number to a given number of decimal places?
by Roy Johnson (Monsignor) on Nov 04, 2003 at 16:04 UTC

This other answer, by wrvhage, has the great property of working correctly for values like 3.005 rounded to two places (where sprintf is not). However, it needs a couple of tweaks to work for negative numbers.

```sub stround
{
my( \$n, \$places ) = @_;
my \$sign = (\$n < 0) ? '-' : '';
my \$abs = abs \$n;
\$sign . substr( \$abs + ( '0.' . '0' x \$places . '5' ), 0, \$places
++ length(int(\$abs)) + 1 );
}

Re: How do I print/round a number to a given number of decimal places?
by wrvhage (Sexton) on Apr 17, 2002 at 13:33 UTC

```sub stround
{
my( \$number, \$decimals ) = @_;
substr( \$number + ( '0.' . '0' x \$decimals . '5' ), 0, \$decimals +
+ length(int(\$number)) + 1 );
}
TIMTOWTDI!

Re: How do I print/round a number to a given number of decimal places?
by littleskittles (Initiate) on Oct 01, 2014 at 05:30 UTC
This answer by Roy Johnson improved this answer by wrvhage by dealing with negative numbers. This answer adds an improvement to handle the case where decimal places is zero, i.e. by not including the hanging decimal point, e.g. "10.1" rounded to zero places would become "10." instead of "10". Any other changes are just rearrangement of the code.
```sub stround
{
my (\$n, \$places) = @_;
my \$abs = abs \$n;
my \$val = substr(\$abs + ('0.' . '0' x \$places . '5'),
0,
length(int(\$abs)) +
((\$places > 0) ? \$places + 1 : 0)
);
(\$n < 0) ? "-" . \$val : \$val;
}
Re: How do I print/round a number to a given number of decimal places?
by Munkey (Novice) on Aug 26, 2015 at 22:34 UTC
Perhaps not as fast as printf, but pretty fast, and not using anything special:
```sub round {

my (\$nr,\$decimals) = @_;
return (-1)*(int(abs(\$nr)*(10**\$decimals) +.5 ) / (10**\$decimals))
+ if \$nr<0;
return int( \$nr*(10**\$decimals) +.5 ) / (10**\$decimals);

}
strange that no-one has posted this, seems like the most straightforward idea to me...
Re: How do I print/round a number to a given number of decimal places?
by Anonymous Monk on Jul 07, 2004 at 14:45 UTC
444.7767655

Originally posted as a Categorized Answer.

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

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

Results (77 votes). Check out past polls.

Notices?