Perl Best Practices [Electronic resources]

Damian Conway

نسخه متنی -صفحه : 317/ 56
نمايش فراداده

4.7. Long Numbers

Use underscores to improve the readability of long numbers .

Large numbers can be difficult to sanity check:

$US_GDP              = 10990000000000;
$US_govt_revenue     =  1782000000000;
$US_govt_expenditure =  2156000000000;

Those figures are supposed to be in the trillions, but it's very hard to tell if they have the right number of zeros. So Perl provides a convenient mechanism for making large numbers easier to read: you can use underscores to "separate your thousands":

# In the US they use thousands, millions, billions, trillions, etc...
$US_GDP = 10_990_000_000_000; $US_govt_revenue = 1_782_000_000_000; $US_govt_expenditure = 2_156_000_000_000;

Prior to Perl 5.8, these separators could only be placed in front of every third digit of an integer (i.e., to separate the thousands, millions, billions, etc.). From 5.8 onwards, underscores can be placed between

any two digits. For example:

# In India they use lakhs, crores, arabs, kharabs, etc...
$India_GDP = 30_33_00_00_00_000; $India_govt_revenue = 86_69_00_00_000; $India_govt_expenditure = 1_14_60_00_00_000;

Separators can also now be used in floating-point numbers and non-decimals, to make them easier to comprehend as well:

use bignum; $PI = 3.141592_653589_793238_462643_383279_502884_197169_399375; $subnet_mask= 0xFF_FF_FF_80;