Addition Subtraction Multiplication and Division in Perl

Home > Search
  by

Addition

This will print "5.35".

print 5.1 + 1.4;

 

+= can be used to add to an already set variable. In this example, $i will equal 3.

$i = 1;
$i += 2;

print $i;

 

This will round the result to a whole number.

use integer;
print 5.1 + 1.4;

Subtraction

This will print "4.85".

print 5.1 - 1.4;

 

-= can be used to subtract from an already set variable. In this example, $i will equal 1.

$i = 3;
$i -= 2;

print $i;

 

This will round the result to a whole number.

use integer;
print 5.1 - 1.4;

 


Multiplication

This will print "7.14".

print 5.1 * 1.4;

 

This will round the result to a whole number.

use integer;
print 5.1 * 1.4;

Division

This will print "1.275".

print 5.1 / 1.4;

 

This will round the result to a whole number.

use integer;
print 5.1 / 1.4;

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments