Lucas
Lucas

Reputation: 14129

How can I get digits out of a Perl bignum?

I have a really big number in Perl. I use "bignum". How can I extract single digits out of this big number. For example if I have a number like this and what to get the 3rd digit from the end:

1029384710985234058763045203948520945862986209845729034856

-> 8

Upvotes: 3

Views: 510

Answers (2)

Lars Haugseth
Lars Haugseth

Reputation: 14881

The bignum package uses Math::BigInt under the hood for integers.

From the Math::BigInt man page:

$x->digit($n);        # return the nth digit, counting from right

Note that counting starts at 0 for the rightmost digit.

Upvotes: 8

Jeremy Smyth
Jeremy Smyth

Reputation: 23503

bignums are transparently available, so this will work:

$digit = substr($bignum, -3, 1);

Upvotes: 11

Related Questions