JavaScript 中的整数 [英] Integers in JavaScript

查看:29
本文介绍了JavaScript 中的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Javascript 的初学者,如果我听起来很愚蠢,请原谅我,因为我从 W3Fools 学习了一些 Javascript(这些教程真的很难 -他们没有解释我想知道的任何事情,但我可能从我使用 C++ 的经验中猜到的一切).

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++).

我可能会切换到 MDN,但如果您可以推荐任何其他教程,那就是很棒.

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

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

Anyways, so here's my question:

我刚刚阅读了几行 this,显然:

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

JavaScript 中的数字是双精度 64 位格式 IEEE754 个值",根据规范.这有一些有趣的结果.没有这样的事情JavaScript 中的整数,所以你有小心你的算术,如果你习惯用 C 来计算的话或 Java.

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.

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

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?

推荐答案

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

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.

Firefox 4 将支持类型化数组,您可以在其中拥有 real 整数数组,例如:https://developer.mozilla.org/en/JavaScript_typed_arrays

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

在此之前,有一些骇人听闻的方法将整数数组存储为字符串,并使用 charCodeAt() 提取每个整数.

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

这篇关于JavaScript 中的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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