【说站】js将小数转为整数的方法
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
本站发布的内容若侵犯到您的权益,请邮件联系站长删除,我们将及时处理!
从您进入本站开始,已表示您已同意接受本站【免责声明】中的一切条款!
本站大部分下载资源收集于网络,不保证其完整性以及安全性,请下载后自行研究。
本站资源仅供学习和交流使用,版权归原作者所有,请勿商业运营、违法使用和传播!请在下载后24小时之内自觉删除。
若作商业用途,请购买正版,由于未及时购买和付费发生的侵权行为,使用者自行承担,概与本站无关。