Javascript中常用的几个数字函数

以下内容摘自《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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值