Search code examples
javascriptroundingstrip

Javascript - Remove leading zero from string


This is probably a super easy one. But I've got a series of numbers which looks like this:

0.12704174228675136
0.3629764065335753

I want to remove the leading 0. and keep only the two first numbers, so this is the result:

12
36

But I don't know how to round numbers . Usually, I'd convert it to a string and use regular expression. I guess I could do that now as well, but since I've got numbers here, I mind as well learn this now.


Solution

  • If they're already strings, substr'll do the trick:

    str.substr(2, 2);
    

    If they're numbers, you can multiply and use Math.floor:

    Math.floor(number * 100);