首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

JavaScript数目字运算

2012-09-10 
JavaScript数字运算Number()、parseInt()、parseFloat()的区别:Number()的强制类型转换与parseInt()和parseF

JavaScript数字运算
Number()、parseInt()、parseFloat()的区别:

Number()的强制类型转换与parseInt()和parseFloat()方法的处理方式相似,只是它转换的是整个值,而不是部分值。parseInt()和parseFloat()方法只转换第一个无效字符之前的字符串。如“3.4.5”被转换成“3.4”,

用Number()进行强制类型转换将返回NAN,
如果字符串值能被完整地转换,Number()将判断是调用parseInt()还是parseFloat()。

var aa = 2.3362; document.write(aa.toFixed(1));                 // 2.3 document.write(aa.toFixed(2));                 // 2.34 document.write(aa.toPrecision(2));             // 2.3document.write(aa.toPrecision(3));             // 2.34  document.write(Math.round(-4.60));             // -5document.write(Math.round(aa * 10) / 10);      // 2.3 document.write(Math.round(aa * 100) / 100);    // 2.34  

热点排行
Bad Request.