# toDigits() function for Int

**URL:** <https://discuss.kotlinlang.org/t/todigits-function-for-int/15298>\
**Category:** Libraries\
**Created:** [December 4, 2019, 7:45pm UTC](https://discuss.kotlinlang.org/t/todigits-function-for-int/15298 "2019-12-04T19:45:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Linus789](https://avatars.discourse-cdn.com/v4/letter/l/e47c2d/32.png) [@Linus789](https://discuss.kotlinlang.org/u/Linus789)\
**Post date:** [December 4, 2019, 7:45pm UTC](https://discuss.kotlinlang.org/t/todigits-function-for-int/15298/1 "2019-12-04T19:45:48Z")

</div>

Hello everyone,

I’m really missing a useful function in Kotlin. Right now the only way to convert an Int to a List of Digits (Int) is to create a method or use toString which is rather ugly.  
Could you provide a function for the Int class which converts a number to its digits via modulo 10 and dividing by 10?

Thanks

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [December 5, 2019, 3:28am UTC](https://discuss.kotlinlang.org/t/todigits-function-for-int/15298/2 "2019-12-05T03:28:38Z")

</div>

I don’t really think it’s necessary. This is so rarely used I can’t think of any real usage of this so it is overkill for standard library.

---

<div class="post-metadata">

**Author:** ![nbness1337](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@nbness1337](https://discuss.kotlinlang.org/u/nbness1337)\
**Post date:** [December 5, 2019, 4:57am UTC](https://discuss.kotlinlang.org/t/todigits-function-for-int/15298/3 "2019-12-05T04:57:55Z")

</div>

`fun Int.toDigits(): List<Int> = toString().map { it.toString().toInt() }`

Unfortunately this is as close as you can really get. That’s why you write your own extension methods for it. You only have to write the ugly once and you can write it your nice way every other time! The joy of programming.

---

<div class="post-metadata">

**Author:** ![imanushin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/imanushin/32/5471_2.png) [@imanushin](https://discuss.kotlinlang.org/u/imanushin)\
**Post date:** [December 5, 2019, 2:46pm UTC](https://discuss.kotlinlang.org/t/todigits-function-for-int/15298/4 "2019-12-05T14:46:29Z")

</div>

> [@Linus789](#):
>
> Right now the only way to convert an Int to a List of Digits (Int) is to create a method or use toString which is rather ugly.

It can be non-optimal (if JVM doesn’t have exact this optimization).

Why is it ugly?

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [December 5, 2019, 3:21pm UTC](https://discuss.kotlinlang.org/t/todigits-function-for-int/15298/5 "2019-12-05T15:21:56Z")

</div>

I guess the fact that you need to call `toString().toInt()` instead of calling `toInt()` directly is a bit annoying, but you only have to write this once so I don’t care.

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [December 5, 2019, 4:25pm UTC](https://discuss.kotlinlang.org/t/todigits-function-for-int/15298/6 "2019-12-05T16:25:02Z")

</div>

Coroutine version:

```kotlin
fun Int.toDigits(base: Int = 10): List<Int> = sequence {
    var n = this@toDigits
    require(n >= 0)
    while (n != 0) {
        yield(n % base)
        n /= base
    }
}.toList()

```

Items are ordered as unit, tens and so on.  
You can use `asReversed()` to get the Arab convention.

---

<div class="post-metadata">

**Author:** ![Arainty](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@Arainty](https://discuss.kotlinlang.org/u/Arainty)\
**Post date:** [December 22, 2024, 4:21pm UTC](https://discuss.kotlinlang.org/t/todigits-function-for-int/15298/7 "2024-12-22T16:21:09Z")

</div>

A version that doesn’t return an empty list with `0` as an input

```kotlin
fun Int.toDigits(base: Int = 10): List<Int> = sequence {
    var n = this@toDigits
    require(n >= 0)
    if (n == 0) yield(n)
    while (n != 0) {
        yield(n % base)
        n /= base
    }
}.toList()

```
