以下内容摘自《ppk on javascript》 一书:
Math.round() always rounds to the nearest integer:
alert(Math.round(1.4)); // yields 1; the nearest integer alert(Math.round(1.6)); // yields 2; the nearest integer ---------------------------------------------------------
Math.floor() always rounds down (including when working with negative numbers):
alert(Math.floor(1.6)); // yields 1 alert(Math.floor(-1.6)); // yields -2
Math.ceil() always rounds up (including when working with negative numbers):
alert(Math.ceil(1.6)); // yields 2 alert(Math.ceil(-1.6)); // yields -1
---------------------------------------------------------
Math.random()
Math.random() gives a random number between 0 and 1. I use it in XMLHTTP Speed Meter to generate a random animation
---------------------------------------------------------toFixed()
The toFixed() method converts a number to a string with the specified amount of decimals. This is especially useful when working with prices; for example, price.toFixed(2) gives output that's suitable for a price.
---------------------------------------------------------
parseInt() and parseFloat()
Two global methods wrap up this quick overview: parseInt() and parseFloat(). Both receive a string as an argument and extract a number: an integer and a floating-point number, respectively.