# Compiler can not parse integer with leading zero

**URL:** <https://discuss.kotlinlang.org/t/compiler-can-not-parse-integer-with-leading-zero/7913>\
**Category:** Language Design\
**Created:** [May 25, 2018, 5:49am UTC](https://discuss.kotlinlang.org/t/compiler-can-not-parse-integer-with-leading-zero/7913 "2018-05-25T05:49:20Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrfox](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@jrfox](https://discuss.kotlinlang.org/u/jrfox)\
**Post date:** [May 25, 2018, 5:49am UTC](https://discuss.kotlinlang.org/t/compiler-can-not-parse-integer-with-leading-zero/7913/1 "2018-05-25T05:49:20Z")

</div>

No lines containing an integer literal with a leading zero will compile or execute in the REPL:

Linux\> ./kotlinc/bin/kotlinc  
Welcome to Kotlin version 1.2.40 (JRE 1.8.0\_91-8u91-b14-1~bpo8+1-b14)  
Type :help for help, :quit for quit

> > > 1  
> > > 1  
> > > 01  
> > > … ;  
> > > error: unexpected tokens (use ‘;’ to separate expressions on the same line)  
> > > 01  
> > > ^  
> > > error: expecting an element  
> > > ;  
> > > ^

---

<div class="post-metadata">

**Author:** ![jstuyts](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstuyts/32/2141_2.png) [@jstuyts](https://discuss.kotlinlang.org/u/jstuyts)\
**Post date:** [May 25, 2018, 6:19am UTC](https://discuss.kotlinlang.org/t/compiler-can-not-parse-integer-with-leading-zero/7913/2 "2018-05-25T06:19:30Z")

</div>

I think a leading zero is not allowed because a lot of developers may think that the number is in octal then, and octal numbers are not supported: [https://kotlinlang.org/docs/reference/basic-types.html#literal-constants](https://kotlinlang.org/docs/reference/basic-types.html#literal-constants)

So a leading zero is used to indicate that you want to have another numeral system, and you have to specify which system you want: `b` for binary and `x` for hexadecimal.

---

<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:** [May 25, 2018, 12:09pm UTC](https://discuss.kotlinlang.org/t/compiler-can-not-parse-integer-with-leading-zero/7913/3 "2018-05-25T12:09:52Z")

</div>

Yes. I was just looking at the [kotlin-spec](https://github.com/JetBrains/kotlin-spec/blob/master/kotlin-spec.asc#decimal-literals) (altough it is no longer up to date). It states

> It is an error if a decimal literal other than 0 starts with digit 0. [Example: Literals 00, 007 are errors. End Example] [Rationale: Some programming languages use a leading zero to denote octal literals. Kotlin does not support octal literals, so without this rule there would be a possibility of difficult-to-catch mistakes when a user enters a literal starting with a leading zero and intends it to be interpreted as an octal literal, but the compiler simply drops leading zeros and interprets it as a decimal literal with a different numeric value than intended. End Rationale]

---

<div class="post-metadata">

**Author:** ![jrfox](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@jrfox](https://discuss.kotlinlang.org/u/jrfox)\
**Post date:** [May 26, 2018, 2:30pm UTC](https://discuss.kotlinlang.org/t/compiler-can-not-parse-integer-with-leading-zero/7913/4 "2018-05-26T14:30:16Z")

</div>

var x = -01 also fails, and surely is harder to justify as an octal halucination deserving a parse failure.

---

<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:** [May 26, 2018, 2:37pm UTC](https://discuss.kotlinlang.org/t/compiler-can-not-parse-integer-with-leading-zero/7913/5 "2018-05-26T14:37:14Z")

</div>

If you look at the specs, this is explained as well. There are no negative number literals in kotlin. `-1` is handled as a unary minus operator and a positive number one. Therefore the same rules with leading zeros apply. Also how would you write a negative number using octal if not as `-01`?

---

<div class="post-metadata">

**Author:** ![jrfox](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@jrfox](https://discuss.kotlinlang.org/u/jrfox)\
**Post date:** [May 31, 2018, 3:25am UTC](https://discuss.kotlinlang.org/t/compiler-can-not-parse-integer-with-leading-zero/7913/6 "2018-05-31T03:25:03Z")

</div>

Well, the answer is simple enough – when you enter octal digits on the control panel of your 8080 computer (typically to jump to the tape reader subroutine), there is no “minus” togle; the concept is missing in both reality and philosophy.

What I am trying to say is that the response to:

opt\> echo “var x = 01;” \> test.kts  
opt\> time ./kotlinc/bin/kotlinc test.kts

should not be:

test.kts:1:10: error: property getter or setter expected  
var x = 01;  
^  
test.kts:1:10: error: unsupported [literal prefixes and suffixes]  
var x = 01;  
^  
opt\> time ./kotlinc/bin/kotlinc test.kts test.kts:1:10: error: property getter or setter expected  
var x = 01;  
^  
test.kts:1:10: error: unsupported [literal prefixes and suffixes]  
var x = 01;  
^

real 0m56.445s  
user 0m28.050s  
sys 0m9.530s

is insufficiant to octal programmers and everyone else.

---

<div class="post-metadata">

**Author:** ![smallufo](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/smallufo/32/1618_2.png) [@smallufo](https://discuss.kotlinlang.org/u/smallufo)\
**Post date:** [August 25, 2021, 6:55pm UTC](https://discuss.kotlinlang.org/t/compiler-can-not-parse-integer-with-leading-zero/7913/7 "2021-08-25T18:55:10Z")

</div>

I hope the leading zero can be supported . As it make our code neater and cleaner :

![截圖 2021-08-26 02.53.16](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/f/f7fb243cc1b8b46590b534cdaed05d78520020ff.png)

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [August 25, 2021, 7:25pm UTC](https://discuss.kotlinlang.org/t/compiler-can-not-parse-integer-with-leading-zero/7913/8 "2021-08-25T19:25:28Z")

</div>

Simple alternative: use numbers prefixed by \_, and so you’ll need something like this:

```auto
const val _0 = 0
const val _1 = 1
const val _2 = 2
const val _3 = 3
const val _4 = 4
const val _5 = 5
const val _6 = 6
const val _7 = 7
const val _8 = 8
const val _9 = 9

```

and yeah it’ll do the job just fine.

---

<div class="post-metadata">

**Author:** ![smallufo](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/smallufo/32/1618_2.png) [@smallufo](https://discuss.kotlinlang.org/u/smallufo)\
**Post date:** [August 25, 2021, 8:03pm UTC](https://discuss.kotlinlang.org/t/compiler-can-not-parse-integer-with-leading-zero/7913/9 "2021-08-25T20:03:24Z")

</div>

> [@kyay10](#):
>
> ```auto
> const val _0 = 0
> 
> ```

well , for 3-digit , I have to define :

```auto
const val __0 = 0

```

and for 4-digit :

```auto
const val ___0 = 0

```

It seems not so clever… 🙄

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [August 25, 2021, 8:20pm UTC](https://discuss.kotlinlang.org/t/compiler-can-not-parse-integer-with-leading-zero/7913/10 "2021-08-25T20:20:42Z")

</div>

If you’re at the point of using 3 or 4 digit numbers then I really don’t think readability matters anymore for single digits. But y’know what, sure:

```auto
inline val Int.i : Int get() = this

```

For readability, it basically looks like it is `i` for Int, I hope that that’s clear. Now, this can infinitely add 2 characters to any number, and so:

```auto
// 1 digit
0
// 2 digit
_0
// 3 digit
0.i
// 4 digit
_0.i
// 5 digit
0.i.i
// 6 digit
_0.i.i
// 7 digit
0.i.i.i
// 8 digit
_0.i.i.i
// 9 digit
0.i.i.i.i
// 10 digit
_0.i.i.i.i
// And so on...

// Lines up perfectly
println(6000000000)
println(_5.i.i.i.i)

```

The truth of the matter is that if you really really need ot squeeze that readability benefit from matching the line widths, then having those `.i` calls shouldn’t really hurt you that much.

Edit: and if you really care about seeing the digit in the last position instead of the first, then consider this implementation instead:

```auto

const val _0 = 0
const val _1 = 1
const val _2 = 2
const val _3 = 3
const val _4 = 4
const val _5 = 5
const val _6 = 6
const val _7 = 7
const val _8 = 8
const val _9 = 9

const val __0 = 0
const val __1 = 1
const val __2 = 2
const val __3 = 3
const val __4 = 4
const val __5 = 5
const val __6 = 6
const val __7 = 7
const val __8 = 8
const val __9 = 9

object I {
    const val _0 = 0
    const val _1 = 1
    const val _2 = 2
    const val _3 = 3
    const val _4 = 4
    const val _5 = 5
    const val _6 = 6
    const val _7 = 7
    const val _8 = 8
	const val _9 = 9
    
    const val __0 = 0
    const val __1 = 1
    const val __2 = 2
    const val __3 = 3
    const val __4 = 4
    const val __5 = 5
    const val __6 = 6
    const val __7 = 7
    const val __8 = 8
	const val __9 = 9
    
    val I: I get() = this
}

```

it works like this:

```auto
// 1 digit
0
// 2 digit
_0
// 3 digit
__0
// 4 digit
I._0
// 5 digit
I.__0
// 6 digit
I.I._0
// 7 digit
I.I.__0
// 8 digit
I.I.I._0
// 9 digit
I.I.I.__0
// 10 digit
I.I.I.I._0
// And so on...

// Lines up perfectly
println(6000000000)
println(I.I.I.I._0)

```

---

<div class="post-metadata">

**Author:** ![smallufo](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/smallufo/32/1618_2.png) [@smallufo](https://discuss.kotlinlang.org/u/smallufo)\
**Post date:** [August 27, 2021, 9:18pm UTC](https://discuss.kotlinlang.org/t/compiler-can-not-parse-integer-with-leading-zero/7913/11 "2021-08-27T21:18:26Z")

</div>

such tweaks are too bizarre…  
I just hope kotlin compiler can support it. As java support this

---

<div class="post-metadata">

**Author:** ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)\
**Post date:** [August 27, 2021, 10:06pm UTC](https://discuss.kotlinlang.org/t/compiler-can-not-parse-integer-with-leading-zero/7913/12 "2021-08-27T22:06:52Z")

</div>

> [@smallufo](#):
>
> As java support this

Well… Java considers them octals, which is likely not what you want.
