当前位置: 首页 > 网络学院 > 客户端脚本教程 > JavaScript > JS Math
The Math object allows you to perform common mathematical tasks.
数学对象允许你来执行一般数学上的任务(一些数学上的运算)
round()
How to use round().
怎样使用round() [四舍五入]
random()
How to use random() to return a random number between 0 and 1.
使用random()来返回一个0与1之间的随机数字
max()
How to use max() to return the number with the highest value of two specified numbers.
使用max()来返回两数字里的最高值(返回比较大的数字)
min()
How to use min() to return the number with the lowest value of two specified numbers.
返回两值中的较小值
Tip: You will find a lot more examples in the Math object reference - look at the bottom of this page!
提示:页底有更多的数字对象参考
The Math object allows you to perform common mathematical tasks.
数学对象可以让你执行一些普通的数学运算
The Math object includes several mathematical values and functions. You do not need to define the Math object before using it.
数学对象包括数学值和函数。你不需要在使用前定义数学对象
JavaScript provides eight mathematical values (constants) that can be accessed from the Math object. These are: E, PI, square root of 2, square root of 1/2, natural log of 2, natural log of 10, base-2 log of E, and base-10 log of E.
JS提提供了八个能从数学对象直接访问的数学值(常数)。它们是:E,PI,平方,根号,log2,log10,base-2 log of E, and base-10 log of E
You may reference these values from your JavaScript like this:
你可以用JS来参考这些值:
Math.E |
In addition to the mathematical values that can be accessed from the Math object there are also several functions (methods) available.
除了使用数学值还可以用数学对象里的几个函数( 方法)
Examples of functions (methods):
The following example uses the round() method of the Math object to round a number to the nearest integer:
下面的举例使用了round()方法来四舍五入
document.write(Math.round(4.7)) |
The code above will result in the following output:
上面的代码会有以下的结果:
5 |
The following example uses the random() method of the Math object to return a random number between 0 and 1:
下面的例子使用了数学对象的random()方法来返回一个0到1之间的随机数字:
document.write(Math.random()) |
The code above can result in the following output:
代码的结果为:
The following example uses the floor() and random() methods of the Math object to return a random number between 0 and 10:
接下来的例子使用了floor()和random()方法来返回一个0到10之间的随机数字:
document.write(Math.floor(Math.random()*11)) |
The code above can result in the following output:
输出结果为:
For a complete reference of all the properties and methods that can be used with the Math object, go to our complete Math object reference.
Date[日期]对象中的所有属性和方法参数,我们将在 完整数学对象参数 中罗列说明。
The reference contains a brief description and examples of use for each property and method!
我们将列举简要说明和典型案例来讲解每个参数的属性和方法的用法