I have a serial number, for example 100101. which represents a particular item but I also have 200101, 300101, 400101... but the related documents depend on whether the first digit is 1, 2, 3 or 4.
With charAt
in a string I can remove the first character, that is, I have 'abcd'
and with charAt(0)
removes me 'a'
, is there something similar to charAt
but for numbers?
The simplest thing would be to pass the number to text and keep the first character:
I could pass the number to String and get the first value:
I leave you a solution without converting to string using a bit of recursion
or its shortened form :)
As an alternative to the above methods (although it's really another way of doing it similar to what Pablo Lozano or Jonathan ch suggest), you could convert the number to a string by concatenating it with an empty string and then accessing the first element using bracket notation . Something like this:
Working directly with numbers would be: first obtain the number of digits that the number has and multiply it by 10 for each digit.
then we divide the number with the number of digits-1 to get the first digit
Example:
charAt
There is even another way, using the with methodcall
on an instance that is notString
:Although you can't use
You can use