Perl: int() function: isolating a digit

Tutoring computer science, extracting a digit is a suggested exercise. The tutor shows a use of Perl’s int() function.

Let’s imagine you want to find the hundred-thousands digit of an input number. You can do so with the following code:

$digit=(int($input_number/100000))%10;

In the code above, the $input_number is first divided by 100000, which will likely give a decimal. Then, int() simply removes the decimal places, but doesn’t round. Next, %10 gives the remainder when the result is divided by 10. This is needed if the input number is a place past the hundred thousands; eg., the millions, ten millions, etc.

HTH:)

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

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

Leave a Reply