gpt4 book ai didi

javascript - 函数 parseInt (1/10000000) 返回 1。为什么?

转载 作者:数据小太阳 更新时间:2023-10-29 05:44:14 26 4
gpt4 key购买 nike

为什么 parseInt(1/10000000) 结果为 1,当 parseInt(1/1000000) 结果为 0?

我需要一些类似于 Java 的 int 转换的东西,比如 int i = -1/10000000;,即 0

我应该使用 Math.floor 表示正值,使用 Math.ceil 表示负值吗?或者有其他解决方案吗?

最佳答案

起初这个问题似乎很有趣。然后我查看了 1/10000000 是什么。

< 1/10000000
> 1e-7

因此:

< parseInt("1e-7"); // note `parseInt` takes a STRING argument
> 1

如果你想 chop 为一个整数,你可以这样做:

function truncateToInteger(real) {
return real - (real % 1);
}

关于javascript - 函数 parseInt (1/10000000) 返回 1。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27650459/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com