Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
86
rated 0 times [  87] [ 1]  / answers: 1 / hits: 21300  / 11 Years ago, thu, november 21, 2013, 12:00:00

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



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}.



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

More From » javascript

 Answers
4

Divide by 1e11 and take the floor:



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 with the 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.


[#74137] Wednesday, November 20, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cortez

Total Points: 326
Total Questions: 106
Total Answers: 110

Location: Slovenia
Member since Wed, Apr 6, 2022
2 Years ago
;