Scala arithmetic operations in detail

 530 total views,  5 views today

Welcome to Scala series. In this tutorial, you will learn about Scala arithmetic operations in detail.

Scala arithmetic operations

Arithmetic operations are basically mathematical operations such as Addition.subtraction,division,multiplication and modulo.

Below are the basic arithmetic operations supported by Scala

  1. + (add two values and generate the result)
  2. – (Subtract two values and generate the result)
  3. * (Multiple two values and generate the result)
  4. / (Divide two values and generate the result)
  5. % (Returns the reminder)

In Scala, each arithmetic operation is a function.That is ,it can be invoked with .+() notation

 result = test.+(10)

This is also equal to,

  result = test + 10

Both are valid and it depends on the readability. I prefer is the second option as it is very simple to use and understand.

Let us see an example for each operation.

Multiply example

 
var test: Int = 100;

 //Multiply
 var result = test.*(2) // Option 1

 println(result)

 result = test * 2 // Option 2

 println(result)

Output

200
200

Addition example

var test: Int = 100;

 //Addition
 var result = test.+(2) // Option 1

 println(result)

 result = test + 2 // Option 2

 println(result)

Output

102
102

Subtraction example

var test: Int = 100;

 //Subtraction
 var result = test.-(2) // Option 1

 println(result)

 result = test - 2 // Option 2

 println(result)

Output

98
98

Division example

var test: Int = 100;

 //Subtraction
 var result = test./(2)

 println(result)

 result = test / 2 

 println(result)

Output

50
50

Modulo example

var test: Int = 100;

 //Subtraction
 var result = test.%(2)

 println(result)

 result = test % 2 

 println(result)

Output

0
0

Complete example of arithmetic operations

 object StaticReference {

  def main(args: Array[String]) {

    var test: Int = 100;

    //Multiply
    var result = test.*(2)

    println(result)

    result = test * 2

    println(result)

    //Addition

    result = test.+(10)

    println(result)

    result = test + 10

    println(result)

    //Subtraction

    result = test.-(10)

    println(result)

    result = test - 10

    println(result)

    //Division

    result = test./(10)

    println(result)

    result = test / 10

    println(result)
   
  }
}

References Official documentation

Thanks for reading. Please leave a comment and give us a thumbs up!.

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.

37 thoughts on “Scala arithmetic operations in detail

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.