从JavaScript中删除小数部分的方法是使用 parseInt()
函数或者 Math.floor()
函数。
parseInt()
函数:parseInt()
函数可以将字符串转换为整数,从而实现删除小数部分的目的。例如:
const num = 123.456;
const numWithoutDecimal = parseInt(num);
console.log(numWithoutDecimal); // 输出 123
Math.floor()
函数:Math.floor()
函数可以将数字向下取整,从而实现删除小数部分的目的。例如:
const num = 123.456;
const numWithoutDecimal = Math.floor(num);
console.log(numWithoutDecimal); // 输出 123
这两种方法都可以从JavaScript中删除小数部分,具体使用哪种方法取决于您的需求和场景。
领取专属 10元无门槛券
手把手带您无忧上云