Question

For example, getting "5" in "256". The closest I've gotten is Math.floor(256/10)), but that'll still return the numbers in front. Is there any simple way to get what I want or would I have to make a big function for it? Also, for clarity: "n digit" would be defined. Example, getDigit(2,256) would return 5 (second digit)

Was it helpful?

Solution

Math.floor((256 / 10) % 10)

or more generally:

Math.floor(N / (Math.pow(10, n)) % 10)

where N is the number to be extracted, and n is the position of the digit. Note that this counts from 0 starting from the right (i.e., the least significant digit = 0), and doesn't account for invalid values of n.

OTHER TIPS

how about

(12345 + "")[3] 

or

(12345 + "").charAt(3)

to count from the other end

[length of string - digit you want] so if you want the 2 it's:

5 - 4 = 1 
(12345 + "")[1] = "2"


function getNumber (var num, var pos){
  var sNum = num + "";

  if(pos > sNum.length || pos <= 0){return "";}

  return sNum[sNum.length - pos]; 

}

First, you need to cast the number to a string, then you can access the character as normal:

var num = 256;
var char = num.toString()[1]; // get the 2nd (0-based index) character from the stringified version of num

Edit: Note also that, if you want to access it without setting the number as a variable first, you need a double dot .. to access the function:

var char = 256..toString()[1];

The first dot tells the interpreter "this is a number"; the second accesses the function.

Convert to string and substring(2,2)?

This should do it:

function getDigit ( position, number ) {
  number = number + ""; // convert number to string
  return number.substr ( position + 1, 1 ); // I'm adding 1 to position, since 0 is the position of the first character and so on
}

Try this, last line is key:

var number = 12345;
var n = 2;
var nDigit = parseInt((number + '').substr(1,1));

If you want to try to do everything mathematically:

var number = 256;
var digitNum = 2;
var digit = ((int)(number/(Math.pow(10,digitNum-1))%10;

This code counts the digit from the right starting with 1, not 0. If you wish to change it to start at 0, delete the -1 portion in the call.

If you wish to count from the left, it gets more complicated and similar to other solutions:

var number = 256;
var digitNum = 2;
var digit = ((int)(number/(Math.pow(10,number.tostring().length-digitNum))%10;

edit:

Also, this assumes you want base 10 for your number system, but both of those will work with other bases. All you need to do is change instances of 10 in the final line of code to the number representing the base for the number system you'd like to use. (ie. hexadecimal =16, binary = 2)

// You do not say if you allow decimal fractions or negative numbers- 
// the strings of those need adjusting.

Number.prototype.nthDigit= function(n){
    var s= String(this).replace(/\D+/g,'');
    if(s.length<=n) return null;
    return Number(s.charAt(n))
}

use variable "count" to control loop

var count = 1; //starting 1
for(i=0; i<100; i++){
  console.log(count);
  if(i%10 == 0) count++;
}

output will fill 1 2 3 4 5 6 7 8 9

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top