如何理解javascript中Math.floor的真正含义?

2026-01-01 01:55:13

1、math.floor(x) 

x为一个数字

作用:它会返回一个不大于x的最大正整数

以下是一些示例

其中console.log()用于控制台输出

如何理解javascript中Math.floor的真正含义?

如何理解javascript中Math.floor的真正含义?

2、示例代码,包含了html类型声明和其他所有标签 可完全复制

<!DOCTYPE html>

<head>

<script>

    console.log( "这是Math.floor(1.4)的结果:"+Math.floor(1.4))

    console.log( "这是Math.floor(1.9)的结果:"+Math.floor(1.9))

    console.log( "这是Math.floor(2)的结果:"+Math.floor(2))

    console.log( "这是Math.floor(2.5)的结果:"+Math.floor(2.5))

    console.log( "这是Math.floor(-1)的结果:"+Math.floor(-1))

</script>

</head>

如何理解javascript中Math.floor的真正含义?

3、一些用途

这段代码会返回不大于a的随机数

Math.floor(Math.random()*a);

示例中a为100

如何理解javascript中Math.floor的真正含义?

如何理解javascript中Math.floor的真正含义?

如何理解javascript中Math.floor的真正含义?

如何理解javascript中Math.floor的真正含义?

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢