Swift - Operator Subtraction Operator

Introduction

The subtraction operator - subtracts one number from another.

You need to be aware of its behavior when subtracting two numbers of different types.

The following subtracts an integer from another:

print(7 - 8)       //integer subtraction (-1)

If you subtract an integer from a double, the result is a double:

print(9.1 - 5)     //double subtraction (4.1)

When you subtract a double from an integer, the result is a double:

print(9 - 4.1)     //double subtraction (4.9)

The subtraction operator can also be used as a unary operator to indicate a negative number:

print(-25)       //negative number

It can be used to negate the value of a variable:

var positiveNum = 5
var negativeNum = -positiveNum  //negativeNum is now -5
positiveNum = -negativeNum      //positiveNum is now 5

Related Topic