8个简单的JavaScript数字方法
web前端开发
共 1963字,需浏览 4分钟
·
2020-11-21 23:51
1、Tostring
const num = 123;
num.toString();
//“ 123”
(100 + 44).toString();
//“ 144”
2、Toexponential
const num = 3.414;
num.toExponential(2);
//3.414e+0
3、Tofixed
const num = 3.414;
num.toFixed(0);
// 3
um.toFixed(2);
//3.41
4、Toprecision
const num = 3.414;
num.toPrecision(2);
//3.4
5、Valueof
const num = 123;
num.valueOf();
// 123
6、Number
Number(true); //returns 1
Number(false); //returns 0
Number("10"); //returns 10
Number("10.43"); //returns 10.43
Number("2,54"); //returns NaN
7、Parseint
parseInt("10"); //returns 10
parseInt("10.43"); //returns 10
parseInt("10 20 30"); //returns 10
parseInt("1000 cupcakes"); //returns 1000 & not delicious cupcakes
parseInt("dogs 1000"); //returns NaN
8、ParseFloat
parseFloat("10"); //returns 10
parseFloat("10.43"); //returns 10.43
parseFloat("10 20 30"); //returns 10
parseFloat("1000 dogs"); //returns 1000
parseFloat("dogs 1000"); //returns NaN
结论
评论