ES6 – Convert To Integer

There are various ways to convert a string or a floating point number to an integer in JavaScript, but not all of them may suit your needs. In this post we will examine four different ways and then we will see which is the best in all cases.

Solution #1: ~~x

If we are dealing with smaller number then we can leverage bit-wise operators such as bit-wise negation (~). The reason we can only use this solution for smaller numbers is because bit-wise operations can only be executed on a number in the range of -(231) to 231 - 1. If the number is outside of that range it will be converted to a number within that range (eg. -2147483649 becomes 2147483647 and 2147483648 becomes -2147483648).

Solution #2: parseInt(x, 10)

There is actually a function that will convert strings into numbers called parseInt(). We can use this function and specify the base to ensure that numbers starting with 0 will not be parsed as octals (eg. parseInt('010') produces 8 in older versions of ECMAScript).

Solution #3: +parseFloat(x, 10).toFixed(0)

We can also use the equivalent function to parseInt() for floats which is parseFloat(). After that we can use Number.prototype.toFixed() to make the number a string representation of the truncated number. Finally we can prefix this expression with a plus sign (+) to convert the string to a number again.

Solution #4: Math.trunc(x)

We can use the newer Math.trunc() function which should in theory just do what we want 😆 .

Results

Below are the results of using your browser to test out each solution:

    Conclusion

    If you have looked through the results above on a modern browser (one implementing Math.trunc()) you’ll notice that for all of our test cases, it is the only solution that works for them all. Therefore, if you are looking to convert floating-point numbers to integers or strings to integers, the most convenient and straightforward way may in fact be ES6’s new Math.trunc() function. Let me know what you think and as always, happy coding! 😎

    365.2425 Days in a Year?

    I was looking through some of the posts on Google+ and noticed that one of Douglas Crockford’s posts alluded to the fact that there are 365.2425 days on average in a year. At first I thought that the number should’ve actually been 365.25 due to us seeing a leap year every 4 years, but then I remembered that leap years don’t always happen every 4 years.

    The rule is that a leap year will be a year evenly divisible by 4 and if it is evenly divisible 100 it must also be evenly divisible by 400. Therefore 1896 was a leap year, but the next leap year wasn’t observed until 1904.

    Keeping all of this in mind, let’s see if we come to the same conclusion that there are 365.2425 days in a year on average. First, let’s determine how many days are in the typical 4 year period:
    365 days × 4 + 1 leap day = 1,461 days

    Now let’s determine how many days there actually are in a 400 year time span. Since we already have a rough calculation for 4 years, we can multiply that by 100 and then account for the 3 times the 3 leap days that wouldn’t occur because even though the year would be divisible by 4 and 100 it wouldn’t be divisible by 400:
    1,461 days × 100 - 3 leap days = 146,097 days

    Now let’s simply divide the number of days in a 400 year time span by 400 to get the average amount of days in a year:
    146,097 ÷ 400 = 365.2425

    So now you know why it is said that on average a year is 365.2425 days long. 😎