Skip to main content

Dealing with integer number leading with 0(s) in JavaScript

Published 1 min readMarkdown

parseInt(012, 10) // 10 parseInt(05, 10) // 5

Interesting, huh? That’s because js would treat number leading with zero(s) as octal numeral, so we need to convert it into decimal number and treat it as string. So here is my solution:

// num = 0120
var result = parseInt(num, 10).toString(8)
// result = '120'

7 views

No comments yet. Be the first to comment.

More from this blog