Perl | Number and its Types

A Number in Perl is a mathematical object used to count, measure, and perform various mathematical operations. A notational symbol that represents a number is termed as a numeral. These numerals, in addition to their use in mathematical operations, are also used for ordering(in the form of serial numbers).

Examples:

1, 2, -4, 5.6, -7.9, 057, 1.67e-10, 0xFFFF

These numbers as per their use can be categorized into various types:

  • Integers: Perl integers are represented as decimal number with base 10. These numbers can be positive and negative both. However, Perl uses “_” to represent big integer numbers to increase readability.

    Example: 123,154,454 is represented as 123_154_454

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #!/usr/bin/perl
      
    # Positive Integer
    $x = 20;
      
    # Negative Integer
    $y = -15;
      
    # Big Integer Number
    $z = 123_154_454;
      
    # Printing these numbers
    print("Positive Integer: ", $x, "\n");
      
    print("Negative Integer: ", $y, "\n");
      
    print("Big Integer: ", $z, "\n");

    chevron_right

    
    

    Output:

    Positive Integer: 20
    Negative Integer: -15
    Big Integer: 123154454
    
  • Floating Numbers:
    Floating point numbers in Perl are those which contain integer as well as fractional values separated by a decimal point. The integer part of a Floating Number could be positive or negative both but the fractional value can only be a positive value. Floating Numbers in Perl can be represented in two ways:

    • Fixed Point: In this representation decimal point is fixed. This decimal point denotes the starting of the fractional part.

      Example: 12.5678

    • Scientific: This representation consists of two parts, significand which represents the actual number and exponent which represents the power of 10 with which the significand is multiplied.

      Example: 1.23567e-3 represents 1235.67

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #!/usr/bin/perl
      
    # Positive Floating Number
    $x = 20.5647;
      
    # Negative Floating Number
    $y = -15.2451;
      
    # Scientific value
    $z = 123.5e-10;
      
    # Printing these numbers
    print("Positive Number: ", $x, "\n");
      
    print("Negative Number: ", $y, "\n");
      
    print("Scientific Number: ", $z, "\n");

    chevron_right

    
    

    Output:

    Positive Number: 20.5647
    Negative Number: -15.2451
    Scientific Number: 1.235e-08
    
  • Hexadecimal Numbers
    Hexadecimal numbers are numbers with base 16 i.e. they range from 0-15 represented as 0xa with ‘0x’ preceding the number whereas ‘a’ here is the value of 10 in Hex form. These alphabets range from ‘a’ to ‘f’ representing 10 to 15. Hexadecimal numbers can be both negative and positive.

    Example: 0xe represents 14 in Hex and 0xc represents 12

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #!/usr/bin/perl
      
    # Positive Hexadecimal Number
    $x = 0xc;
      
    # Negative Hexadecimal Number
    $y = -0xe;
      
    # Printing these values
    print("Positive Hex Number: ", $x, "\n");
    print("Negative Hex Number: ", $y, "\n");
      
    # To print Hex value
    printf("Value in Hex Format: %x", $x);

    chevron_right

    
    

    Output:

    Positive Hex Number: 12
    Negative Hex Number: -14
    Value in Hex Format: c
    

    Hence, to print value of the number in Hexadecimal format, ‘%x’ is used as shown above.

  • Octal Numbers
    Octal numbers are the numbers with base 8 i.e. they range from 0-7. These numbers are represented as 057 with ‘0’ preceding the number and the rest part is the octal value of the required decimal number. Just like other types, octal numbers can also be negative as well as positive.

    Example: For octal number 057 the decimal equivalent will be 47.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #!/usr/bin/perl
      
    # Positive Octal Number
    $x = 074;
      
    # Negative Octal Number
    $y = -074;
      
    print("Positive Octal number: ", $x, "\n");
    print("Negative Octal number: ", $y, "\n");
      
    # To print value in Octal Form
    printf("Value in Octal Form: %o", $x);

    chevron_right

    
    

    Output:

    Positive Octal number: 60
    Negative Octal number: -60
    Value in Octal Form: 74
    

    Here, ‘%o’ is used to print the value in its octal form

  • Binary Numbers
    Binary numbers are number with base 2 i.e. they are formed of only two values 0 and 1. These numbers are represented as 0b1010 with ‘0b’ preceding the number.
    Example: For binary number ‘0b1010′ the decimal equivalent will be ’10’.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #!/usr/bin/perl
       
    # Positive Binary Number
    $x = 0b1010;
      
    # Negative Binary Number
    $y = -0b10110;
      
    # Printing these values
    print("Positive Binary Number: ", $x, "\n");
    print("Negative Binary Number: ", $y, "\n");
      
    # Printing in unsigned binary form
    printf("Value in unsigned Binary Form: %b", $x);

    chevron_right

    
    

    Output:

    Positive Binary Number: 10
    Negative Binary Number: -22
    Value in unsigned Binary Form: 1010
    

    In the above code, ‘%b’ is used to print the number in its actual binary form.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.