js中Math对象的方法


1.丢弃小数部分,保留整数部分
parseInt(5/2)

2.向上取整,有小数就整数部分加1

Math.ceil(5/2)

3,四舍五入.

Math.round(5/2)

4,向下取整

Math.floor(5/2)

Math 对象的方法
FF: Firefox, N: Netscape, IE: Internet Explorer

方法 描述 FF N IE 
abs(x) 返回数的绝对值 1 2 3 
acos(x) 返回数的反余弦值 1 2 3 
asin(x) 返回数的反正弦值 1 2 3 
atan(x) 以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值 1 2 3 
atan2(y,x) 返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间) 1 2 3 
ceil(x) 对一个数进行上舍入。 1 2 3 
cos(x) 返回数的余弦 1 2 3 
exp(x) 返回 e 的指数。 1 2 3 
floor(x) 对一个数进行下舍入。 1 2 3 
log(x) 返回数的自然对数(底为e) 1 2 3 
max(x,y) 返回 x 和 y 中的最高值 1 2 3 
min(x,y) 返回 x 和 y 中的最低值 1 2 3 
pow(x,y) 返回 x 的 y 次幂 1 2 3 
random() 返回 0 ~ 1 之间的随机数 1 2 3 
round(x) 把一个数四舍五入为最接近的整数 1 2 3 
sin(x) 返回数的正弦 1 2 3 
sqrt(x) 返回数的平方根 1 2 3 
tan(x) 返回一个角的正切 1 2 3 
toSource() 代表对象的源代码 1 4 - 
valueOf() 返回一个 Math 对象的原始值



<script type="text/javascript">  
//保留两位小数
//功能:将浮点数四舍五入,取小数点后2位
function toDecimal(x) {
var f = parseFloat(x);
if (isNaN(f)) {
return;
}
f = Math.round(x*100)/100;
return f;
}


//制保留2位小数,如:2,会在2后面补上00.即2.00
function toDecimal2(x) {
var f = parseFloat(x);
if (isNaN(f)) {
return false;
}
var f = Math.round(x*100)/100;
var s = f.toString();
var rs = s.indexOf('.');
if (rs < 0) {
rs = s.length;
s += '.';
}
while (s.length <= rs + 2) {
s += '0';
}
return s;
}

function fomatFloat(src,pos){
return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);
}
//四舍五入
alert("保留2位小数:" + toDecimal(3.14159267));
alert("强制保留2位小数:" + toDecimal2(3.14159267));
alert("保留2位小数:" + toDecimal(3.14559267));
alert("强制保留2位小数:" + toDecimal2(3.15159267));
alert("保留2位小数:" + fomatFloat(3.14559267, 2));
alert("保留1位小数:" + fomatFloat(3.15159267, 1));

//五舍六入
alert("保留2位小数:" + 1000.003.toFixed(2));
alert("保留1位小数:" + 1000.08.toFixed(1));
alert("保留1位小数:" + 1000.04.toFixed(1));
alert("保留1位小数:" + 1000.05.toFixed(1));

//科学计数
alert(3.1415.toExponential(2));
alert(3.1455.toExponential(2));
alert(3.1445.toExponential(2));
alert(3.1465.toExponential(2));
alert(3.1665.toExponential(1));
//精确到n位,不含n位
alert("精确到小数点第2位" + 3.1415.toPrecision(2));
alert("精确到小数点第3位" + 3.1465.toPrecision(3));
alert("精确到小数点第2位" + 3.1415.toPrecision(2));
alert("精确到小数点第2位" + 3.1455.toPrecision(2));
alert("精确到小数点第5位" + 3.141592679287.toPrecision(5));
</script>

JS中parseInt()、random()及Math.cell()函数的学习


一、parseInt()函数

表达式:parseInt(string,radio);string为将要转换的字符串,radio为转换的基数。可选。表示要解析的数字的基数。该值介于 2 ~ 36 之间。如果省略该参数或其值为 0,则数字将以 10 为基础来解析。如果它以 “0x” 或 “0X” 开头,将以 16 为基数。如果该参数小于 2 或者大于 36,则 parseInt() 将返回 NaN。

作用:将第一个字符串参数,转换为整数;

返回值:整数;

实例:

parseInt("010",10)就是10进制的结果:10

parseInt("010",2)就是2进制的结果:2

parseInt("010",8)就是8进制的结果:8

parseInt("010",16)就是2进制的结果:16

当没有指定进制单位的时候,默认是10进制,但:如果是里面的Number是0开头的就认为是8进制的,如果是0x开头的就认为是16进制的。

parseInt("10")==>parseInt("010",10)===>10

parseInt("010")==>parseInt("010",8)==>8

parseInt("0x10")==>parseInt("010",16)==>16

 

二、random()函数

random函数语法:Math.random();

random函数参数:无参数

random函数返回值:返回0和1之间的伪随机数,可能为0,但总是小于1,[0,1)

random函数示例:document.write(Math.random());

返回随机数

   document.write(Math.random()*(20-10)+10)  返回指定范围内(10 -20)之间的随机数  

   document.write(Math.random()*(m-n)+n)  返回指定范围内(n -m)之间的随机数  


三、Math.cell()函数

作用:对一个数上舍入
表达式:Math.ceil(x)
参数:x,任意数或表达式。
返回值:大于等于x,并且与它最接近的整数。

描述:

Math.ceil()执行的是向上取整数计算,它返回的是大于或等于函数的参数,并且与之最接近的整数。Math.ceil()执行的操作不同于 Math.round(),Math.ceil()总是向上舍入,而Math.round()可以上舍入或下舍入到接近的整数。还要注 意,Math.ceil()不会将负数舍入为更小的负数,而是向0舍入。

例子

    a = Math.ceil(1.99);   // Result is 2.0
    b = Math.ceil(1.01);   // Result is 2.0
    c = Math.ceil(1.0);    // Result is 1.0
    d = Math.ceil(-1.99);  // Result is -1.0

沁园春 www.qinychun.com








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值