get the number of n digit in a 2+ digit number
Asked Answered
D

9

6

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)

Demurrer answered 23/12, 2010 at 23:11 Comment(2)
You didn't specify whether you are counting from left or from right.Dibri
Many of the answers return the value as a string, not as an integer. Does that matter?Elohim
E
11
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.

Elohim answered 23/12, 2010 at 23:16 Comment(5)
The generalization is incorrect. Just put n=2 to see for yourself.Dibri
@Martin v. Löwis: Thanks. I was distracted at the moment I was writing that.Elohim
Hmm. This still isn't right: you are missing closing parens, and "% n" should likely read "% 10".Dibri
@Martin v. Löwis: Yes, that is what I meant. Thanks again.Elohim
I tested this with javascript: in the URL bar and it worked for me. I also wanted to use a sort of formula (exactly like this) rather than strings so this is my preferable answer.Demurrer
S
11

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]; 

}
Stella answered 23/12, 2010 at 23:16 Comment(1)
Most likely, the OP wants to count from the other end.Dibri
E
11
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.

Elohim answered 23/12, 2010 at 23:16 Comment(5)
The generalization is incorrect. Just put n=2 to see for yourself.Dibri
@Martin v. Löwis: Thanks. I was distracted at the moment I was writing that.Elohim
Hmm. This still isn't right: you are missing closing parens, and "% n" should likely read "% 10".Dibri
@Martin v. Löwis: Yes, that is what I meant. Thanks again.Elohim
I tested this with javascript: in the URL bar and it worked for me. I also wanted to use a sort of formula (exactly like this) rather than strings so this is my preferable answer.Demurrer
R
6

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.

Rosati answered 23/12, 2010 at 23:16 Comment(2)
Most likely, the OP wants to count from the other end.Dibri
I'd like it counted from smallest to largest.Demurrer
A
0

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
}
Acetophenetidin answered 23/12, 2010 at 23:15 Comment(0)
Q
0

Convert to string and substring(2,2)?

Quinate answered 23/12, 2010 at 23:16 Comment(0)
A
0

Try this, last line is key:

var number = 12345;
var n = 2;
var nDigit = parseInt((number + '').substr(1,1));
Acapulco answered 23/12, 2010 at 23:16 Comment(1)
charAt(1) looks better than substr(1,1) for the purpose :)Bordelaise
T
0
// 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))
}
Talaria answered 23/12, 2010 at 23:45 Comment(0)
S
0

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)

Spiker answered 23/12, 2010 at 23:51 Comment(0)
M
0

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

Mapes answered 3/9, 2015 at 3:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.