math.round vs parseInt

The two functions are really quite different.

parseInt() extracts a number from a string, e.g.

parseInt('1.5')
// => 1

Math.round() rounds the number to the nearest whole number:

Math.round('1.5')
// => 2

parseInt() can get its number by removing extra text, e.g.:

parseInt('12foo')
// => 12

However, Math.round will not:

Math.round('12foo')
// => NaN

You should probably use parseFloat and Math.round since you’re getting input from the user:

var number = parseFloat(prompt('Enter number:'));
var rounded = Math.round(number);

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)