Perl | Math::BigInt->length() method Last Updated : 03 Oct, 2019 Comments Improve Suggest changes Like Article Like Report Math::BigInt module in Perl provides objects that represent integers with arbitrary precision and overloaded arithmetical operators. length() method of Math::BigInt module is used to get the length of the given number i.e the count of digits in the given number. Syntax: Math::BigInt->length() Parameter: None Returns: an integer value which represents the length of the given number. Example 1: Use of Math::BigInt->length() method to count digits in a number perl #!/usr/bin/perl # Import Math::BigInt module use Math::BigInt; # Specify number $num = 78215936043546; # Create BigInt object $x = Math::BigInt->new($num); # Get the length (or count of # digits) of the # given number using # Math::BigInt->length() method $len = $x->length(); print "Length of $num is $len.\n"; # Specify another number $num = 7821593604584625197; # Create BigInt object $x = Math::BigInt->new($num); # Get the length (or count of # digits) of the # given number using # Math::BigInt->length() method $len = $x->length(); print "Length of $num is $len.\n"; Output: Length of 78215936043546 is 14. Length of 7821593604584625197 is 19. Example 2: Use of Math::BigInt->length() method to split a given number into two halves. perl #!/usr/bin/perl # Import Math::BigInt module use Math::BigInt; # Specify number $num = 78215936043546; # Create BigInt object $x = Math::BigInt->new($num); # Get the length (or count of # digits) of the # given number using # Math::BigInt->length() method $len = $x->length(); # Variable to store first half $firstHalf = 0; $i = 1; # loop to calculate first half while($i <= ($len/2)) { $firstHalf = ($firstHalf * 10) + $x->digit(-$i); $i = $i + 1; } # Variable to store second half $secondHalf = 0; # Loop to calculate second half while($i <= $x->length()) { $secondHalf = ($secondHalf * 10) + $x->digit(-$i); $i = $i + 1; } # Note: Math::BigInt->digit() method # returns the digit at ith position # from right end of the given number # a negative value of i is used # to get ith digit from left end # of the given number # Print original number print "Original number: $num\n"; # Print first half print "First half: $firstHalf\n"; # Print Second half print "Second half: $secondHalf"; Output: Original number: 78215936043546 First half: 7821593 Second half: 6043546 Comment More infoAdvertise with us Next Article Perl | Math::BigInt->length() method I ihritik Follow Improve Article Tags : Perl Perl-method Perl-Math-Functions Similar Reads Perl | Math::BigInt->digit() method Math::BigInt module in Perl provides objects that represent integers with arbitrary precision and overloaded arithmetical operators. digit() method of Math::BigInt module is used to get nth digit of the given number counting from right. To get the digit from the left side we need to specify n as neg 3 min read Perl | Math::BigInt->bneg() method Math::BigInt module in Perl provides objects that represent integers with arbitrary precision and overloaded arithmetical operators. bneg() method of Math::BigInt module is used to change the value of input to its negative value and does nothing for zero or NAN values Syntax: Math::BigInt->bneg() 1 min read Perl | Math::BigInt->brsft() method Math::BigInt module in Perl provides objects that represent integers with arbitrary precision and overloaded arithmetical operators.brsft() method of Math::BigInt module is used to right shift the given value by a base given as parameter. Syntax: Math::BigInt->brsft()Parameter: $y- number of time 1 min read Perl | Math::BigInt->config() method Math::BigInt module in Perl provides objects that represent integers with arbitrary precision and overloaded arithmetical operators. config() method of Math::BigInt module is used to get the information about the configuration of the Perl module. Syntax: Math::BigInt->config() Parameter: None Ret 1 min read Perl | Math::BigInt->binf() method Math::BigInt module in Perl provides objects that represent integers with arbitrary precision and overloaded arithmetical operators. binf() method of Math::BigInt module is used to create a new object with value infinity and if used on an existing object, it sets it to infinity. Syntax: Math::BigInt 2 min read Like