整数在JavaScript中 [英] Integers in JavaScript

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

问题描述

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



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



不管怎样,这里是我的问题:

我刚刚读了几行这个,显然:


JavaScript中的数字是
精确的64位格式IEEE
754值,根据规范。
这有一些有趣的
后果。在JavaScript中没有
的整数,所以如果你习惯用C
或者Java来计算,那么你有
要小心你的
算术。 / b>

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

解决方案

在JavaScript中只有几种数据类型:对象,数字和字符串。在你读的时候,JS编号都是64位的浮点数。没有ints。

Firefox 4将支持Typed Arrays,您可以使用 real 整数的数组,例如 https://developer.mozilla.org/zh/JavaScript_typed_arrays


$ b $在这之前,有一些方法把整数数组作为字符串存储,然后使用 charCodeAt()来取出每个整数。


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

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

Anyways, so here's my question:

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

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.

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?

解决方案

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 will have support for Typed Arrays, where you can have arrays of real ints and such: 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().

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

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