函数parseInt(1/10000000)返回1。为什么? [英] Function parseInt (1/10000000) returns 1. Why?

查看:68
本文介绍了函数parseInt(1/10000000)返回1。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 parseInt(1/1 /时为什么 parseInt(1/10000000)结果 1 1000000)结果是 0

Why parseInt(1/10000000) results 1, when parseInt(1/1000000) result is 0?

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

I need some analog to Java's int casting like int i = -1/10000000;, Which is 0.

我应该使用 Math.floor 表示正数,使用 Math.ceil 表示负数吗?还是有其他解决方案?

Should I use Math.floor for positive and Math.ceil for negative? Or is there another solution?

推荐答案

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

At first the question seems interesting. Then I looked at what 1/10000000 is.

< 1/10000000
> 1e-7

因此:

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

如果要截断为整数,可以执行以下操作:

If you want to truncate to an integer, you can do this:

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

这篇关于函数parseInt(1/10000000)返回1。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆