JavaScript中Math.trunc()方法的重要性是什么?
Math.trunc()
与Math.floor(),Math.ceil()和Math.round()不同,Math.trunc()方法删除小数部分 ,只给出整数 部分。它不在乎将数字四舍五入 到最接近的整数。甚至不会注意到该值是正数还是负数。它的功能仅仅是将 小数部分切碎。
语法
Math.trunc(x);
参数- Math.trunc()需要数作为参数,并截断的小数部分。
在下面的示例中,Math.trunc()方法将截断所提供正数的分数。
示例
<html>
<body>
<script>
var val1 = Math.trunc("1.414");
var val2 = Math.trunc("0.49");
var val3 = Math.trunc("8.9");
document.write(val1);
document.write("</br>");
document.write(val2);
document.write("</br>");
document.write(val3);
</script>
</body>
</html>输出结果
1 0 8
在下面的Math.trunc() 函数中,将负数的分数值截断并在输出中显示其整数值。
示例
<html>
<body>
<script>
var val1 = Math.trunc("-1.414");
var val2 = Math.trunc("-0.49");
var val3 = Math.trunc("-8.9");
document.write(val1);
document.write("</br>");
document.write(val2);
document.write("</br>");
document.write(val3);
</script>
</body>
</html>输出结果
-1 0 -8
热门推荐
8 薛之谦祝福语简短
10 恋爱祝福语大全简短搞笑
11 姨父生日祝福语简短精辟
12 剪简短的新年祝福语
13 猪年给老师祝福语简短
14 免费生日祝福语简短独特
15 婚礼亲友祝福语简短幽默
16 朋友搬迁新房祝福语简短
17 老婆生日祝福语简短孩子
18 甄嬛传祝福语简短