In Python, I can convert a number to a string in a custom format. For example - hexadecimal, minimum width 5, with leading zeroes:
>>> i = 4660
>>> f'{i:05x}'
'01234'
How do I do the same in JavaScript? Is there a better way than the following?
> var i = 4660
> ('00000' + i.toString(16)).substr(-5)
'01234'
Edit: So, it seems the solution in this specific example is i.toString(16).padStart(5, '0')
. In general, though, is there no sprintf
-like function in JavaScript?