Home » Scala language

Numeric Types in Scala

Numeric type contains integer and floating-point data. In Scala program, Float, Double, Int, Short, Long come under the number type. In this tutorial on numeric types in Scala, we will learn the initialization and functioning of all data types that come under numeric type.
Submitted by Shivang Yadav, on July 16, 2019

Scala Numeric Types

Numeric type in Scala deals with all types that are stored in numerical. The data type that is used is decimals (float and Double) and integers (Int, Short, Long).

1) Int Data Type in Scala

The int data type stores integer variables that take memory location of 4 bytes. The value of int data type ranges from -2147483648 to 2147483647.

Syntax of initialize Integers:

    var a = 343 // Without date type
    var a : Int = 4623 // With data type 

Example code for Int data type

object MyClass {
      def main(args: Array[String]) {
        var ch = 324
        var b : Int = 3342
        println("The value of int ch is " + ch)
        println("Value of integer b is " + b)
      }
   }

Output

The value of int ch is 324
Value of integer b is 3342

2) Short Data Type in Scala

The short data type stores the integer value of variables that takes memory location of 2 bytes. The value of short data type ranges from -32768 to 32767.

Syntax of initialize short Integers

    var a = 343 // Without data type will give int 
    var a : Short = 4623 //without this variable will be integer. 

Example code for short data type

object MyClass {
      def main(args: Array[String]) {
        var b : Short = 3342;
        println("Value of short integer b is " + b);
      }
   }

Output

Value of short integer b is 3342

3) Long Data Type in Scala

The long data type stores integer value to variables that takes memory location of 8 bytes. The value of long data type ranges from -9223372036854775808 to 9223372036854775807.

Syntax of initialize long Integers

    var a = 343 // Without date type will give int 
    var a : Long = 4623 //without this variable will be integer. 

Example code for short data type

object MyClass {
      def main(args: Array[String]) {
        var b : Long = 33423452;
        println("Value of long integer b is " + b);
      }
   }

Output

Value of long integer b is 33423452

4) Float Data Type in Scala

The float data type stores decimal values to its variables that takes memory location of 4 bytes. The value of float data type ranges from -3.4E+38 to +3.4E+38 i.e. single precision.

Syntax of initialize Float values

    var variable_name = 343.43 
    var variable_name : Float = 463.876 

Example code for float data type

object MyClass {
      def main(args: Array[String]) {
        var b : Float = 33.97f;
        println("Value of Float value b is " + b);
      }
   }

Output

Value of Float value b is 33.97

5) Double Data Type in Scala

The double data type stores decimal values to its variables that takes memory location of 8 bytes. The value of float data type ranges from IEEE 754 double-precision float.

Syntax of initialize Float values

    var variable_name = 3463.434565 // this will be initialized as float
    var variable_name : Double = 46763.87634

Example code for double data type

object MyClass {
      def main(args: Array[String]) {
        var b : Double = 345453.978765;
        println("Value of Double value b is " + b);
      }
   }

Output

Value of Double value b is 345453.978765





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.