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():
- Let’s imagine the number is stored in the variable $num.
-
To display the number rounded to its nearest integer, use the command
printf(“%.0f”,$num);
To round with sprintf():
- Let’s imagine the number is stored in the variable $num.
-
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:
McGrath, Mike. Perl in easy steps. Southam: Computer Step, 2004.
Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.
Leave a Reply
You must be logged in to post a comment.