新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

js中如何将小数转为整数

这篇“js中如何将小数转为整数”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“js中如何将小数转为整数”文章吧。

创新互联公司主要从事成都网站建设、成都网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务河南,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

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中如何将小数转为整数”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注创新互联行业资讯频道。


网页标题:js中如何将小数转为整数
转载源于:http://www.wjwzjz.com/article/ijhpih.html
在线咨询
服务热线
服务热线:028-86922220
TOP