js将小数转为整数的方法

298次阅读
没有评论

js将小数转为整数的方法

1、使用“parseInt(小数值)”语句。

document.write(parseInt("10") + "
");
document.write(parseInt("10.33") + "
");
document.write(parseInt("34 45 66") + "
");
document.write(parseInt(" 60 ") + "
");
document.write(parseInt("40 years") + "
");
document.write(parseInt("He was 40") + "
");
document.write("
");
document.write(parseInt("10",10)+ "
");
document.write(parseInt("010")+ "
");
document.write(parseInt("10",8)+ "
");
document.write(parseInt("0x10")+ "
");
document.write(parseInt("10",16)+ "
");

2、使用“~~小数值”语句。

var decimal=4;
var integer = ~~decimal; // 4 = ~~4.123
console.log(integer);

3、使用“Math.floor(小数值)”语句。

console.log(Math.floor(2.5));  //2
console.log(Math.floor(-2.5));  //-3

4、使用“Math.ceil(小数值)”语句。

console.log(Math.ceil(2.5));  //3
console.log(Math.ceil(-2.5));  //-2

5、使用“Math.round(小数值)”语句。

console.log(Math.round(2.5));  //3
console.log(Math.round(-2.5));  //-2
console.log(Math.round(-2.6));  //-3

以上就是js将小数转为整数的方法,希望对大家有所帮助。更多Javascript学习指路:Javascript

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:JavaScript2022-12-15发表,共计924字。
新手QQ群:570568346,欢迎进群讨论 Python51学习