Perl: round off

The tutor gives a way to round numbers in Perl.

To my knowledge, the best way to round a number in Perl is with its built-in printf() or sprintf().

To round with printf():

  1. Let’s imagine the number is stored in the variable $num.
  2. To display the number rounded to its nearest integer, use the command
    printf(“%.0f”,$num);

To round with sprintf():

  1. Let’s imagine the number is stored in the variable $num.
  2. To display the number rounded to its nearest integer, use the command
    print(sprintf(“%.0f”,$num));

For more info about printf() or sprintf(), see my post here.

HTH:)

Source:

Robert’s Perl tutorial

stackoverflow.com

McGrath, Mike. Perl in easy steps. Southam: Computer Step, 2004.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Leave a Reply