JavaScript 中的整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4703725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:34:45  来源:igfitidea点击:

Integers in JavaScript

javascriptfloating-pointinteger

提问by Mateen Ulhaq

I'm a beginner to Javascript so forgive me if I sound dumb because I learned some Javascript from W3Fools(which are really difficult tutorials - they don't explain anything I want to know, but everything I probably can guess from my experience with C++).

我是 Javascript 的初学者,所以如果我听起来很笨,请原谅我,因为我从W3Fools中学到了一些 Javascript (这些教程非常困难 - 他们没有解释我想知道的任何内容,但我可能会从我的 C++ 经验中猜到所有内容)。

I may be switching over to MDN, but if you can recommend any other tutorials, that be great.

我可能会切换到MDN,但如果你能推荐任何其他教程,那就太好了。

Anyways, so here's my question:

无论如何,这是我的问题:

I just read a few lines of this, and apparently:

我只是读了几行this,显然:

Numbers in JavaScript are "double-precision 64-bit format IEEE 754 values", according to the spec. This has some interesting consequences. There's no such thing as an integer in JavaScript, so you have to be a little careful with your arithmetic if you're used to math in C or Java.

根据规范,JavaScript 中的数字是“双精度 64 位格式 IEEE 754 值”。这有一些有趣的后果。JavaScript 中没有整数这样的东西,所以如果你习惯了 C 或 Java 中的数学,你必须小心你的算术。

I've already seen that there are few of the data types (for variables) I'm used to from C++. But I didn't expect all numbers to automatically be floats. Isn't there any way to use integers, not float? Will a future version of JavaScript support ints?

我已经看到我从 C++ 中习惯的数据类型(对于变量)很少。但我没想到所有数字都会自动变为floats。没有办法使用integers,不是float吗?未来版本的 JavaScript 会支持ints 吗?

采纳答案by erjiang

There are really only a few data types in Javascript: Objects, numbers, and strings. As you read, JS numbers are all 64-bit floats. There are no ints.

Javascript 中实际上只有几种数据类型:对象、数字和字符串。正如你所读到的,JS 数字都是 64 位浮点数。没有整数。

Firefox 4 will have support for Typed Arrays, where you can have arrays of realints and such: https://developer.mozilla.org/en/JavaScript_typed_arrays

Firefox 4 将支持 Typed Arrays,您可以在其中拥有真正的int数组,例如:https: //developer.mozilla.org/en/JavaScript_typed_arrays

Until then, there are hackish ways to store integer arrays as strings, and plucking out each integers using charCodeAt().

在那之前,有一些黑客方法可以将整数数组存储为字符串,并使用charCodeAt().

回答by Axel Gneiting

I don't think it ever will support integers. It isn't a problem as every unsigned 32 bit integer can be accurately represented as a 64 bit floating point number.

我认为它永远不会支持整数。这不是问题,因为每个无符号 32 位整数都可以准确地表示为 64 位浮点数。

Modern JavaScript engines could be smart enough to generate special code when the numbers are integer (with safeguard checks to make sure of it), but I'm not sure.

当数字为整数时,现代 JavaScript 引擎可能足够智能以生成特殊代码(通过安全检查来确保它),但我不确定。

回答by qwertymk

Use this:

用这个:

function int(a) { return Math.floor(a); }

And yo can use it like this:

你可以这样使用它:

var result = int(2.1 + 8.7 + 9.3); //int(20.1)
alert(result);                     //alerts 20

回答by qwertymk

There is zero support for integers. It should be a simple matter of rounding off a variable every time you need it. The performance overhead isn't bad at all.

对整数的支持为零。每次需要时四舍五入变量应该是一件简单的事情。性能开销一点也不差。

回答by ryebr3ad

If you really need to use integers, just use the floor method on each number you encounter.

如果您确实需要使用整数,只需对遇到的每个数字使用 floor 方法。

Other than that, the loose typing of Javascript is one of it's strengths.

除此之外,Javascript 的松散类型是它的优势之一。