javascript 从 int 中删除最后一位数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20123976/
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-27 17:48:16  来源:igfitidea点击:

Remove last digits from an int

javascript

提问by Niklas

Can't seem to find a good answer to this question. How do I remove the last 11 digits from an int?

似乎无法找到这个问题的好答案。如何从 int 中删除最后 11 位数字?

The id could be one or more numbers in the beginning but there will always be 11 digits following the id. There will always be an id in the beginning. {id}{11 digits}.

id 开头可以是一个或多个数字,但 id 后面总是有 11 位数字。一开始总会有一个id。{id}{11 digits}.

var getId = function (digits) {
   // Do some stuff and return id
}

getId(110000000001); // Should return 1
getId(1110000000001); // Should return 11
getId(2010000000001); // Should return 20

回答by Pointy

Divide by 1e11and take the floor:

除以1e11发言:

var stripped = Math.floor(id / 1e11);

This avoids conversion to/from a string representation.

这避免了与字符串表示的转换。

Note that the nature of JavaScript numerics is such that your "raw" values (the values withthe 11 useless digits) can't have more than 5 digits in addition to those 11 before you'll start running into precision problems. I guess if you never care about the low-order 11 digits that might not be a problem.

请注意,JavaScript 数字的性质是,在您开始遇到精度问题之前,您的“原始”值(具有11 个无用数字的值)除了 11 位之外不能超过 5 位。我想如果你从不关心低位 11 位数字,那可能不是问题。

回答by Joke_Sense10

Try this:

试试这个:

var str = 1100000000011; 
var res = str.toString().substr(0, str.toString().length - 11);

Demo

演示

回答by Artem Sobolev

You can convert your number to string and delete tailing digits:

您可以将您的数字转换为字符串并删除尾随数字:

digits.toString().replace(/\d{11}$/, '')

By the way, you better don't use ints (or to be precise, Numbers) because if number is greater than 2147483648 (by absolute value), it'll be represented internally as doubleresulting in precision loss. If don't need tailing digits, then it's okay — use division approach suggested in other answers (this one could break because of scientific notation). But if you want to keep all the data, you should represent your numbers with strings.

顺便说一句,您最好不要使用整数(或者准确地说是数字),因为如果数字大于 2147483648(绝对值),它将在内部表示为double导致精度损失。如果不需要尾数,那么没关系 - 使用其他答案中建议的除法方法(由于科学记数法,这个方法可能会中断)。但是如果你想保留所有的数据,你应该用字符串来表示你的数字。

回答by MKII

You can use division and 10^11 to do so. Edit: my bad

您可以使用除法和 10^11 来执行此操作。编辑:我的坏

回答by Maksym Stepanenko

var getId = function (digits) {
   var id = parseInt((digits/(1e11)),0);
}

回答by Shawn G.

You can convert the number to a string and slice the last 11 characters from the end

您可以将数字转换为字符串并从末尾切下最后 11 个字符

parseInt( digits.toString().slice(0, -11) );

parseInt( digits.toString().slice(0, -11) );

Using .slice( 0 , [ negative_number ]);will slice xcharacters from the end of the string

使用.slice( 0 , [ negative_number ]);将从x字符串的末尾切片字符

More information on .slice()Here

关于这里的更多信息.slice()