When I want to select the nth character, I use the charAt() method, but what's the equivalent I can use when dealing with integers instead of string values?
-
what prevents you from using charAt()? – naveen Jan 11 '11 at 06:12
-
charAt only works on strings, so you have to convert it first. – majelbstoat Jan 11 '11 at 06:20
8 Answers
Use String()
:
var number = 132943154134;
// convert number to a string, then extract the first digit
var one = String(number).charAt(0);
// convert the first digit back to an integer
var one_as_number = Number(one);

- 45,033
- 10
- 85
- 120
-
1Just a note on this very old question - all these string parsing techniques may not work for very large numbers. I was using that approach and it failed because the string representation was in scientific notation. – nasch Jan 01 '20 at 17:27
-
There are some solutions to formatting really big numbers in here -> https://stackoverflow.com/q/1685680…i.e. `number.toLocaleString('fullwide', {useGrouping: false});`. – Seth Dec 02 '21 at 19:36
It's a stupid solution but seems to work without converting to string.
var number = 123456789;
var pos = 4;
var digit = ~~(number/Math.pow(10,pos))- ~~(number/Math.pow(10,pos+1))*10;

- 3,551
- 1
- 28
- 24
-
That's a nice succinct way of expressing in code what I suggested in another answer -- http://stackoverflow.com/questions/4654715/how-do-i-select-the-nth-digit-in-a-large-integer-inside-javascript/4654765#4654765 -- but like in my prose description, "pos" is counting positions from the right. You'd need to subtract pos from log base 10 of number to count from the left, which is what's expected here. – metamatt Jan 11 '11 at 16:28
You could convert the number to a string and do the same thing:
parseInt((number + '').charAt(0))

- 12,889
- 4
- 28
- 26
function digitAt(val, index) {
return Math.floor(
(
val / Math.pow(10, Math.floor(Math.log(Math.abs(val)) / Math.LN10)-index)
)
% 10
);
};
digitAt(123456789, 0) // => 1
digitAt(123456789, 3) // => 4
A bit messy.
Math.floor(Math.log(Math.abs(val)) / Math.LN10)
Calculates the number of digits (-1) in the number.

- 2,485
- 19
- 19
If you want an existing method, convert it to a string and use charAt.
If you want a method that avoids converting it to a string, you could play games with dividing it by 10 repeatedly to strip off enough digits from the right -- say for 123456789, if you want the 3rd-from-right digit (6), divide by 10 3 times yielding 123456, then take the result mod 10 yielding 6. If you want to start counting digits from the left, which you probably do, then you need to know how many digits (base 10) are in the entire number, which you could deduce from the log base 10 of the number... All this is unlikely to be any more efficient than just converting it to a string.

- 13,809
- 7
- 46
- 56
var number = 123456789
function return_digit(n){
r = number.toString().split('')[n-1]*1;
return r;
}
return_digit(3); /* returns 3 */
return_digit(6); /* returns 6 */

- 5,068
- 44
- 38
-
-
downvote because you can't write `.split()` in JS. Question doesn't ask for pure JS. I write JS. You read pure JS! – Kareem Nov 10 '18 at 15:14
-
For you info: today we can write [.split()](https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/String/split) in pure JS (with only the standard library) but in your answer you write `in Jquery` – Et7f3XIV Nov 10 '18 at 16:43
var num = 123456;
var secondChar = num.toString()[1]; //get the second character

- 1,148
- 1
- 13
- 20
Nobody said about negative integers :)
For non negative integers
(integer+'')[index]
For negative integers
(integer+'')[index + 1]
const f = (integer, index) => (''+integer)[integer < 0 ? index + 1 : index];
[number, position].forEach(input => input.addEventListener('input', e => {
integer = +number.value;
index = +position.value;
result.innerHTML = f(integer, index);
}))
<input type="number" id="number" placeholder="number" value="0"/>
<input type="number" id="position" placeholder="position" value="0" min="0"/>
<div id="result"></div>

- 1,502
- 2
- 7
- 23