JavaScript学习与实践(23)

 js中的Math,他用于完成一般的数学作业。

还是先给出几个例子,希望大家能够多加指正我的错误

   1,怎么用round()

<html>
<body>
<script type="text/javascript">
document.write(Math.round(0.60) + "<br />")
document.write(Math.round(0.50) + "<br />")
document.write(Math.round(0.49) + "<br />")
document.write(Math.round(-4.40) + "<br />")
document.write(Math.round(-4.60))
</script>
</body>
</html>
大家可以去运行一下,结果是:

1
1
0
-4
-5

2怎么用random()来返回一个0和1之间的随机数

<html>
<body>
<script type="text/javascript">
document.write(Math.random())
</script>
</body>
</html>
大家自己去测试一下结果
3,大家用max()来返回比较数中的最大的数
<html>
<body>
<script type="text/javascript">
document.write(Math.max(5,7) + "<br />")
document.write(Math.max(-3,5) + "<br />")
document.write(Math.max(-3,-5) + "<br />")
document.write(Math.max(7.25,7.30))
</script>
</body>
</html>
结果是:
 

7
5
-3
7.3

4,大家用min()来返回比较数中最小的一个
<html>
<body>
<script type="text/javascript">
document.write(Math.min(5,7) + "<br />")
document.write(Math.min(-3,5) + "<br />")
document.write(Math.min(-3,-5) + "<br />")
document.write(Math.min(7.25,7.30))
</script>
</body>
</html>
结果是:
 

5
-3
-5
7.25

下面是这个对象中所有的方法和属性

 

Math Object Methods

FF: Firefox, N: Netscape, IE: Internet Explorer

MethodDescriptionFFNIE
abs(x)Returns the absolute value of a number123
acos(x)Returns the arccosine of a number123
asin(x)Returns the arcsine of a number123
atan(x)Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians123
atan2(y,x)Returns the angle theta of an (x,y) point as a numeric value between -PI and PI radians123
ceil(x)Returns the value of a number rounded upwards to the nearest integer123
cos(x)Returns the cosine of a number123
exp(x)Returns the value of Ex123
floor(x)Returns the value of a number rounded downwards to the nearest integer123
log(x)Returns the natural logarithm (base E) of a number123
max(x,y)Returns the number with the highest value of x and y123
min(x,y)Returns the number with the lowest value of x and y123
pow(x,y)Returns the value of x to the power of y123
random()Returns a random number between 0 and 1123
round(x)Rounds a number to the nearest integer123
sin(x)Returns the sine of a number123
sqrt(x)Returns the square root of a number123
tan(x)Returns the tangent of an angle123
toSource()Represents the source code of an object14-
valueOf()Returns the primitive value of a Math object124


Math Object Properties

PropertyDescriptionFFNIE 
constructorA reference to the function that created the object124
EReturns Euler's constant (approx. 2.718)123
LN2Returns the natural logarithm of 2 (approx. 0.693)123
LN10Returns the natural logarithm of 10 (approx. 2.302)123
LOG2EReturns the base-2 logarithm of E (approx. 1.414)123
LOG10EReturns the base-10 logarithm of E (approx. 0.434)123
PIReturns PI (approx. 3.14159)123
prototypeAllows you to add properties and methods to the object124
SQRT1_2Returns the square root of 1/2 (approx. 0.707)123
SQRT2Returns the square root of 2 (approx. 1.414)123

转载于:https://www.cnblogs.com/lxsohu/archive/2007/01/31/635655.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值