Numbers in Kotlin
Tags: kotlin, todayilearned, codenewbie, beginners
Published: 2019-07-12 19:53:42

* Kotlin handles numbers in a way close to Java, but not exactly the same. For example, there are no widening conversions for numbers, and literals are slightly different in some cases.

**The numbers of data type can be divided into two categories:**
1. Whole numbers
2. Fractional numbers

##Whole Numbers
---
* The number without fractions is called the whole number.
* *Note that characters are not numbers in kotlin.* 
* Remember for whole numbers ***Int*** is a default. That you mean if you assign the whole number without specifying its type then it will be considered as an Int by default.

**There are basically four types of whole numbers in kotlin.**
1. Byte — 8 Bit
2. Short — 16 Bit
3. Int — 32 Bit
4. Long — 64 Bit

####Integer Literal Constants

There are following kinds of literal constants for integer values.
* Decimals — 123
* Longs are tagged by capital L — 123 L
* Hexadecimal — 0x0F
* Binaries — 0b00001011
* **Octal literals are not supported.**

##Fractional numbers
---
* The number with a precision point is called fractional numbers.

There are two types of fractional numbers:
1. Float — 32 Bit
2. Double — 64 Bit

* Float provides you single-precision 32-bit floating-point value. It provides a precision of 6 digits after the decimal point.

* Remember for fractional numbers ***double*** is a default. That you mean if you assign fractional number without specifying its type then it will be considered as double by default.

* Double provides you double-precision 64-bit floating-point value. It provides a precision of 12 to 16 digits after decimal point (which may vary).

####Fractional Literal Constants
* There are following kinds of literal constants for fractional values.
* Kotlin also supports a conventional notation for floating-point numbers.
* Doubles: 123.5, 123.5e10
* Floats are tagged by **F** or **f**: 123.5f, 154.5F

##Number Super Class
---
* Imagine the situation where you don’t know which kind of number will be input by the user. Many languages do not have the solution for this kind of situations. Except for kotlin.

* In kotlin, you can use Number super-class and make variable as a Number type. So no matter which kind of number user will input. Your variable can accept both integers and double as well.
```
fun main(args: Array<String>) {
    var a: Number = 45  // Works fine with number super class.
    var b: Number = 45.20
    print(a)
    print(b)
}
```

##Underscores in numeric literals (since Version 1.1)
---
* You can use underscores to make number constants more readable:
```
val oneMillion = 1_000_000
val creditCardNumber = 1234_5678_9012_3456L etc.
```

So, guys, that’s it for numbers in kotlin. Feel free to let me know if I missed something.

Till then Keep Coding, Keep Loving. Catch you up in another article.

Jai Hind, Vande Mataram 🇮🇳

Wanna get in touch with me? Here are links. I’ll love to become your friend. 😊

- [My Site] (https://www.jaytillu.in)
- [My Blogs] (https://blogs.jaytillu.in)
- [Twitter](https://twitter.com/jay_tillu)
- [Facebook](https://www.facebook.com/jaytillu.1314/)
- [Instagram](https://www.facebook.com/jaytillu.1314/)
- [Medium](https://medium.com/jay-tillu)
- [LinkedIn](https://www.linkedin.com/in/jaytillu/)
- [Github](https://www.github.com/jay-tillu)
- [Stack overflow](https://stackoverflow.com/users/8509590/jay-tillu)
or just mail me at jd.tillu@gmail.com