Node:Numeric Literals, Previous:Numbers, Up:Numbers



Numeric Literals

Numeric literals are simply constant numbers. Numeric literals are much easier to comprehend and use than string literals. There are only a few basic ways to express numeric literals.

The numeric literal representations that Perl users are similar to those used in other languages such as C, Ada, and Pascal. The following are a few common examples:

     42;         # The number 42
     12.5;       # A floating point number, twelve and a half
     101873.000; # 101,873
     .005        # five thousandths
     5E-3;       # same number as previous line
     23e-100;    # 23 times 10 to the power of -100 (very small)
     2.3E-99;    # The same number as the line above!
     23e6;       # 23,000,000
     23_000_000; # The same number as line above
                 # The underscores are for readability only
     

As you can see, there are three basic ways to express numeric literals. The most simple way is to write an integer value, without a decimal point, such as 42. This represents the number forty-two.

You can also write numeric literals with a decimal point. So, you can write numbers like 12.5, to represent numbers that are not integral values. If you like, you can write something like 101873.000, which really simply represents the integral value 101,873. Perl does not mind that you put the extra 0's on the end.

Probably the most complex method of expressing a numeric literal is using what is called exponential notation. These are numbers of the form b * 10^x , where b is some decimal number, positive or negative, and x is some integer, positive or negative. Thus, you can express very large numbers, or very small numbers that are mostly 0s (either to the right or left of the decimal point) using this notation. However, when you write such a number as a literal in Perl, you must write it in the from bEx, where b and x are the desired base and exponent, but E is the actual character, E (or e, if you prefer). The examples of 5E-3, 23e-100, 2.3E-99, and 23e6 in the code above show how the exponential notation can be used.

Finally, if you write out a very large number, such as 23000000, you can place underscores inside the number to make it more readable. 1 Thus, 23000000 is exactly the same as 23_000_000.


Footnotes

  1. Language historians may notice that this is a feature from the Ada language.