javascript 获取 2+ 位数字中的 n 位数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4523131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 13:59:31  来源:igfitidea点击:

get the number of n digit in a 2+ digit number

javascriptnumbersdigit

提问by Anonymous

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)

例如,在“256”中得到“5”。我得到的最接近的是 Math.floor(256/10)),但这仍然会返回前面的数字。有没有什么简单的方法可以得到我想要的东西,或者我必须为它做一个大的功能吗?此外,为了清楚起见:将定义“n 位”。例如, getDigit(2,256) 将返回 5(第二个数字)

回答by kemiller2002

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:

[length of string - digit you want]所以如果你想要 2 它是:

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

}

回答by Dan Breslau

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

or more generally:

或更一般地说:

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

where Nis the number to be extracted, and nis 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.

其中N是要提取n的数字, 是数字的位置。请注意,这从 0 从右侧开始计数(即,最低有效数字 = 0),并且不考虑 的无效值n

回答by lonesomeday

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.

第一个点告诉解释器“这是一个数字”;第二个访问函数。

回答by Jan Han?i?

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
}

回答by Mud

Convert to string and substring(2,2)?

转换为字符串和子字符串(2,2)?

回答by Orbling

Try this, last line is key:

试试这个,最后一行是关键:

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

回答by kennebec

// 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))
}

回答by Matt

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.

此代码从右侧开始计算数字,从 1 开始,而不是 0。如果您希望将其更改为从 0 开始,请删除调用中的 -1 部分。

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)

此外,这假设您的数字系统需要以 10 为基数,但这两种方法都适用于其他基数。您需要做的就是将最后一行代码中的 10 实例更改为代表您要使用的数字系统的基数的数字。(即十六进制=16,二进制=2)

回答by hellovietnam

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

输出将填满 1 2 3 4 5 6 7 8 9