JScript Math 对象

Math 对象有许多内部属性和方法。 这些属性是特定的数字。

使用 Math 对象

这些特定数字之一就是 pi 的值(大约为 3.14159...)。 这是 Math.PI 属性的值,下面的示例中就使用了该属性。

// A radius variable is declared and assigned a numeric value.
var radius = 4;
var area = Math.PI * radius * radius;
// Note capitalization of Math and PI.

Math 对象的一种内置方法为幂方法,或 pow,它将数字提升到指定的幂次。 下面的示例使用了 pi 和幂的计算。

// This formula calculates the volume of a sphere with the given radius.
var volume = (4/3)*(Math.PI*Math.pow(radius,3));

另一个示例如下:

var x = Math.floor( Math.random()*10 ) + 1;

不能显式构造 Math 对象;此对象始终对程序可用。

请参见

参考

Math 对象

其他资源

内部对象