数字:
通过e加数字的方式表示小数点后多少位。
eg:
1e3 = 1,000;
1.2e6 = 1,200,000;
1e-6 = 0.000001;
toString(基峰)
由于返回方法的进制字符串;
eg:let num =255;
alert(num.tostring(16) );//ff
alert(num.toString(2) );//11111111
内置函数:
Math.floor(向下取整);
Math.ceil(向上取整);
Math.round(取最接近的整数);
Math.trunc(删除小数点后的内容,直接取整)
解决精确度失真的最后办法就是通过 toFixed(n)来舍入结果
parseInt(读取数字并返回整型)parseFloat(读取数字并返回浮点型)
字符串开头或结尾为空格时会被忽略,
只有开头为数值才可读,遇非数字则结束读取
eg:
alert (parseInt('1000px') );//100 alert (parseFloat('12.5em');//12.5 alert(parseInt('a123') );
parseInt(str,radix);
parseInt( )函数有可选的第二参数。
因此parseInt也可以解析十六精制数,与二进制数等字符串。
eg:
alert( parseInt('0xff', 16) );//255 alert( parseInt('ff', 16) );//255 alert( parseInt('2n9c', 36) );//123456
其他函数:
Math.random( )
返回0到1之间的随机数(不包括1);Math.max(a, b, c...)/Math.min(a, b, c...)
从任意数量的参数中返回最大/最小值;Math.pow(n, power)
返回提高了给定的功率;
习题:
重复输入,直到输入为数字为止:
function readNumber() { let num; do { num = prompt("Enter a number please?", 0); } while ( !isFinite(num) ); if (num === null || num === '') return null; return +num; } alert(`Read: ${readNumber()}`);
本文章借鉴自https://javascript.info;