parseInt versus Math.round

In JavaScript, when converting an input to a number, you have a couple of choices, you can use the built-in parseInt() function or the built-in Math.round() function to get an integer style number from a string.

Math.round will round up or down to the nearest integer value, and while it supposedly works on numbers, it (like everything in JavaScript it seems) can be called with a string variable:

parseInt('12.8') == 12
Math.round('12.8') == 13

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s