Kotlin Number Tutorial With Example

Kotlin Number is super class for all platform classes representing numeric values (such as Byte, Int, Long, Short etc.).

How to Declare and Define Number in Kotlin

You can declare a kotlin number variable as below –

var firstNum: Number

Here, we have declared a Number variable firstNum.

You can define this variable as below –

firstNum = 9

Here, we have defined the Number variable firstName by assigning a value 9 to it.

You can declare and define a Kotlin Number variable in same line as below –

var firstNum: Number = 9

Here, we have declared a Number variable firstNum and defined it by assigning a value 9 to it.

– You can not compare a Number variable with character data type. Let’s see an example,

fun main(args: Array<String>) {
   var firstNum: Number = 9
   if(firstNum == '9') {
       print("firstNum is equal to 9")
   } else {
       print("firstNum is not equal to 9")
   }
}

When you run above program, you will get an error –
Operator ‘==’ cannot be applied to Number and Char

Frequently Used Functions in Kotlin Number Data Types

There are some functions that are used frequently for data type conversion. They are –
1. toByte()
2. toChar()
3. toDouble()
4. toFloat()
5. toInt()
6. toLong()
7. toShort()

1. toByte() function

This functions returns the value of the number as a Byte which may involve rounding or truncation.

Declaration of toByte() function

abstract fun toByte(): Byte

Example to show use of toByte() function –

fun main(args: Array<String>) {

   var numb: Number = 60;

   print("Number is $numb \n")

   var numbByte = numb.toByte()
   print("Number in byte is $numbByte")
}

Output –
Number is 60
Number in byte is 60

Note – Byte represents 8 bit signed integer.

2. toChar() function

This function returns Character that has numeric value equal to this numeric value, which may be truncated to 16 bits if appropriate.

Declaration of toChar() function –

abstract fun toChar(): Char

Example to show use of toChar() function –

fun main(args: Array<String>) {

   var numb: Number = 60

   print("Number is $numb \n")

   var numbChar = numb.toChar()
   print("Number in char is $numbChar")
}

Output –
Number is 60
Number in char is < Note - ASCII value of character < is 60. So, numb.toChar() method returns character <.

3. toDouble() function

This function returns the numeric value as a Double which may involve rounding.

Declaration of toDouble() function –

abstract fun toDouble(): Double

Example to show use of toDouble() function –

fun main(args: Array<String>) {

   var numb: Number = 60;

   print("Number is $numb \n")

   var numbDouble = numb.toDouble()
   print("Number in double is $numbDouble")
}

Output –
Number is 60
Number in double is 60.0

4. toFloat() function

This function returns the numeric value as Float which may involve rounding.

Declaration of toFloat() function –

abstract fun toFloat(): Float

Example to show use of toFloat() function –

fun main(args: Array<String>) {

   var numb: Number = 60;

   print("Number is $numb \n")

   var numbFloat = numb.toFloat()
   print("Number in float is $numbFloat")

Output –
Number is 60
Number in float is 60.0

5. toInt() function

This function returns numeric value as Int which may involve rounding or truncation.

Declaration of toInt() function –

abstract fun toInt(): Int

Example to show use of toInt() function –

fun main(args: Array<String>) {

   var numb: Number = 60;

   print("Number is $numb \n")

   var numbInt = numb.toInt()
   print("Number in int is $numbInt")
}

Output –
Number is 60
Number in int is 60

6. toLong() function

This function returns numeric value as Long which may involve rounding or truncation.

Declaration of toLong() function –

abstract fun toLong(): Long

Example to show use of toLong() function –

fun main(args: Array<String>) {

   var numb: Number = 60;

   print("Number is $numb \n")

   var numbLong = numb.toLong()
   print("Number in long is $numbLong")
}

Output –
Number is 60
Number in long is 60

7. toShort() function

This function returns numeric value as Short which may involve rounding or truncation.

Declaration of toShort() function –

abstract fun toShort(): Short

Example to show use of toShort() function –

fun main(args: Array<String>) {

   var numb: Number = 60;

   print("Number is $numb \n")

   var numbShort = numb.toShort()
   print("Number in short is $numbShort")
}

Output –
Number is 60
Number in short is 60

A complete example to show use of different functions in Kotlin Number Data Types –

fun main(args: Array<String>) {

   var numb: Number = 90;

   print("Number is $numb \n")

   var numbByte = numb.toByte()
   print("Number in byte is $numbByte \n")

   var numbChar = numb.toChar()
   print("Number in char is $numbChar \n")

   var numbDouble = numb.toDouble()
   print("Number in double is $numbDouble \n")

   var numbFloat = numb.toFloat()
   print("Number in float is $numbFloat \n")

   var numbInt = numb.toInt()
   print("Number in int is $numbInt \n")

   var numbLong = numb.toLong()
   print("Number in long is $numbLong \n")

   var numbShort = numb.toShort()
   print("Number in short is $numbShort \n")
}

Output –
Number is 90
Number in byte is 90
Number in char is Z
Number in double is 90.0
Number in float is 90.0
Number in int is 90
Number in long is 90
Number in short is 90

Some Inherited function in Kotlin Number –

1. equal – This function is used to check whether any object is equal to this one or not.
Declaration –

open operator fun equals(other: Any?): Boolean

2. hashCode – This function returns hash code value for this object.
Declaration –

open fun hashCode(): Int

3. toString – This function returns string representation of the object.
Declaration –

open fun toString(): String

Exercises on Kotlin Number –

1. Guess the output of below program –

fun main(args: Array<String>) {

   var numb: Number = 1000;

   print("Number is $numb \n")

   var numbByte = numb.toByte()
   print("Number in byte is $numbByte \n")

   var numbChar = numb.toChar()
   print("Number in char is $numbChar \n")

   var numbDouble = numb.toDouble()
   print("Number in double is $numbDouble \n")

   var numbFloat = numb.toFloat()
   print("Number in float is $numbFloat \n")

   var numbInt = numb.toInt()
   print("Number in int is $numbInt \n")

   var numbLong = numb.toLong()
   print("Number in long is $numbLong \n")

   var numbShort = numb.toShort()
   print("Number in short is $numbShort \n")
}

2. Guess the output of below program –

fun main(args: Array<String>) {

   var numb: Number = 100000;

   print("Number is $numb \n")

   var numbByte = numb.toByte()
   print("Number in byte is $numbByte \n")

   var numbChar = numb.toChar()
   print("Number in char is $numbChar \n")

   var numbDouble = numb.toDouble()
   print("Number in double is $numbDouble \n")

   var numbFloat = numb.toFloat()
   print("Number in float is $numbFloat \n")

   var numbInt = numb.toInt()
   print("Number in int is $numbInt \n")

   var numbLong = numb.toLong()
   print("Number in long is $numbLong \n")

   var numbShort = numb.toShort()
   print("Number in short is $numbShort \n")
}

3. Guess the output of below program –

fun main(args: Array<String>) {
   var firstNum: Number = 3
   if(firstNum == '3') {
       print("firstNum is equal to 3")
   } else {
       print("firstNum is not equal to 3")
   }

   print(firstNum)
}

4. Guess the output of below program –

fun main(args: Array<String>) {
  var firstNum: Number = 3
   if(firstNum == "3") {
       print("firstNum is equal to 3")
   } else {
       print("firstNum is not equal to 3")
   }
}

That’s the end of tutorial on Kotlin Number.

Leave a Reply