How to capture the last three digits of a number in Javascript?

Asked

Viewed 198 times

7

Is there any Javascript function that captures from the last character?

For example:

var n = 200100;

I want to get only the 3 numbers from the last character. In the example above, I would give 100.

2 answers

15


If the reported value is of type whole (numerical), capture the rest of the division by 1000:

var n = 200100;
console.log(n % 1000);

If not, if the value is of the type string (non-numeric), use slice:

var n = '200100';
console.log(n.slice(-3));

Alternative nonstandard, using the method substr:

var n = '200100';
console.log(n.substr(-3));

  • 3

    Two details about the use of substr, first that the second parameter in this specific use (negative number) would not be necessary, only this would be enough n.substr(-3). Second, the MDN informs that substr() is obsolete, but I didn’t find any of that in https://262.ecma-international.org/12.0/#sec-string.prototype.substr, maybe the MDN is wrong, so I was in doubt. PS: the mod is much more efficient than any other solution (although the result varies in the Firefox JS engine), already regex had the worst performance: https://jsbench.me/zdkrxhpe2w/1 - Node: https://i.stack.Imgur.com/9PQR7.png

  • 4

    @Guilhermenascimento, the standardization of String.prototype.substr is in the annex B of the specification (reserved for Features obsolete, legacy, etc). So it’s in a "differentiated" category: "This Annex describes Various legacy Features and other Characteristics of web browser Ecmascript hosts." I confess that I did not know about this situation. MDN could have made it clearer, tbh. Here’s more info, if anyone wants.

  • 3

    @Guilhermenascimento. Despite being out of scope I have to leave my admiration for its position. I tell everyone to read it, we have to question it and we have to question ourselves at all times, at all times. Only then will we achieve quality in our production. We are the largest community of developers in Portuguese language and we must break with the social dogma of being perversely kind and accept everything only out of kindness or humility to the detriment of truth, logic and reason.

  • 3

    Hi @Guilhermenascimento, thanks for the complements. In fact the second argument of substr is unnecessary in this case, I will edit to make the code cleaner. I’m also reversing the order of the options I offered to make it clear that the mod is by far the best option in this case. I also did not know about this obsolescence of the substr! I will research the reasons for this, because it is much more practical than the String.prototype.substring in cases like this.

1

You can use a logic like this to get the latest numbers

var n = '200100'

var ultimo = n[n.length - 1]
var segundoUltimo = n[n.length - 2]
var terceiroUltimo = n[n.length - 3]

console.log(terceiroUltimo, segundoUltimo, ultimo)

  • 6

    The vote was not very favorable, but the solution is ingenious and I believe performative. Of course in the scenario of the question is even obvious to do % 1000 or use substr, as answered by @bfavaretto (who already had my up), but using index in this case is still much more sensible than regex (which I already consider to border error in this scenario) or other artifices. In some situations it has yes applicability (if not sequential, for example). What was missing was a little explanation of what [ ] in strings does, would value.

  • 3

    I would like to ask the three people who voted in favour of this answer, what the reason for the votes is because this answer, in addition to imposing unnecessary complexity on a simple activity, getting the rest of a split, generates a flawed result if the input is less than 100.

  • 2

    @Augustovasques I voted in view of Bacco’s comment. The score was very disproportionate (-5 at the time of my vote), and in fact this technique is ingenious, even if it is not the best solution to the specific case of the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.