New Document
PERL - Numbers

Numbers are scalar data. They exist in PERL as real numbers, float, integers, exponents, octal, and hexidecimal numbers.
$real = 27;
$float = 3.14159;
$integer = -4;
$exponent = 10e12;
PERL - Mathematical Functions

With numbers comes math. Simple arithmetic operations are discussed in the PERL Operators lesson.

Some mathematical functions require some additional PERL Modules. Here's a few trigonomic functions that will only function if your build of PERL has the Math::Trig module installed.
use Math::Trig; #USE THIS MODULE

print "content-type: text/html \n\n"; #HTTP HEADER
$real = 27;
$float = 3.14159;
$integer = -4;
$exponent = 10e12;
print tan($real);     #TANGENT FUNCTION
print "<br />";
print sin($float);    #SINE FUNCTION
print "<br />";
print acos($integer); #COSINE FUNCTION
PERL - Numbers with Operators

Numbers aren't much without arithmetic operations. This next example is a sneak peak of the next lesson, PERL Operators.

print "content-type: text/html \n\n"; #HTTP HEADER

$x = 14;
$y = 10;
$area = ($x * $y);
print $area;
print "<br />";
PERL - Formatting Numbers

Computers are capable of calculating numbers that you and I probably never knew existed. This is especially true with calculations involving decimals, floating-point numbers, or percentages.

You may find that one of the best solutions is to first convert your numbers when possible to integers (get rid of the decimal). You may then go ahead and perform the required operations such as multiplication, division, addition, or whatever and finally reintroduce the decimal using division.

print "content-type: text/html \n\n"; #HTTP HEADER

$hourlyrate = 7.50; #DECIMAL TO BE RID OF
$hoursworked = 35;
$no_decimal_rate = ($hourlyrate * 100);

$netpay = ($no_decimal_rate * $hoursworked);
$paycheck = ($netpay / 100);

print "Hourly Wage: $hourlyrate<br />";
print "Hours: $hoursworked<br />";
print "No Decimal: $no_decimal_rate<br />";
print "Net Pay: $netpay<br />";
print "Pay Check: $paycheck<br />";
Hourly Wage: 7.5
Hours: 35
No Decimal: 750
Net Pay: 26250
Pay Check: 262.5

In this example we followed the steps stated above, first we removed the decimal from each number involved in the calculation, ($hourlyrate and $hoursworked). Then we performed the operation ($netpay), and finally introduced the decimal again by dividing our $netpay by the same number we used to get rid of the decimal in the first place (100).

  • Convert to real numbers.
  • Perform the operation(s).
  • Convert back to a decimal.
Previous                                                                                                                                                       Next

Back to Top