简体   繁体   English

JavaScript中是否有浮点数?

[英]Are there floating-point numbers in JavaScript?

I see many questions about methods to detect if a value is a floating-point number, but none of the examples work on numbers like as 1.0, 5.0, etc. 我看到许多关于检测值是否是浮点数的方法的问题,但是没有一个例子适用于1.0,5.0等数字。

Is it possible to distinguish between numbers like 1 and 1.0 or 5 and 5.0? 是否可以区分1和1.0或5和5.0之类的数字? If yes, how can you detect a floating-point variable type? 如果是,您如何检测浮点变量类型?

In Ecmascript 6, you can use isInteger : 在Ecmascript 6中,您可以使用isInteger

var value = . . .;
Number.isInteger(value);

However, this is not widely supported yet. 但是,目前尚未得到广泛支持。 For now, you can use this: 现在,你可以使用这个:

function isInteger(value) {
    return typeof value === 'number' && (value | 0) === value;
}

This should work for all integer values that can be represented exactly in a 64-bit floating point representation. 这应该适用于所有可以在64位浮点表示中精确表示的整数值。

I think the more concise answer is this one : 我认为这个问题的答案更为简洁:

In javascript : 5 === 5.0. 在javascript:5 === 5.0。

So from this point on, no way can be found to distinguish them. 所以从这一点来看,无法找到区分它们的方法。 They are strictly the same thing. 它们完全是一回事。

Edit : reminder : the only x such as (x!==x) in javascript is NaN. 编辑:提醒:javascript中唯一的x如(x!== x)是NaN。

Zerstoren, the others are technically right: in javascript if you enter 2.0 or 2, there is no difference on how data is stored internally, so there is no way to find out if data was originally written as integer or float. Zerstoren,其他技术上都是正确的:在javascript中如果输入2.0或2,内部数据的存储方式没有区别,因此无法确定数据最初是写入整数还是浮点数。

However, in case you want to validate some user form data, so that the user enters only integer numbers (eg order 2 pizzas, not 2.0) then the you do have a solution because that's actually a string (hence "2.0" is not same as "2"). 但是,如果您想要验证某些用户表单数据,以便用户只输入整数(例如,订购2个比萨饼,而不是2.0),那么您确实有一个解决方案,因为它实际上是一个字符串(因此“2.0”不相同作为“2”)。 You can use 您可以使用

typeof(n) =="string" && parseInt(n, 10) == n && n.indexOf('.')==-1

The first typeof condition is so that you fall under data is still in string format case I mentioned. 第一种类型的条件是你在数据下仍然是我提到的字符串格式。 See here: http://jsfiddle.net/X6U2d/ 见这里: http//jsfiddle.net/X6U2d/

Andrei 安德烈

There is no such thing as a float or int basic types in JavaScript, only a number . 在JavaScript中没有floatint基本类型这样的东西,只有一个number So, there is no difference between 5.0 and 5. 所以,5.0和5之间没有区别。

If it is important to you (for whatever reason), you will need to store the value as a string then use parseFloat to extract the value. 如果它对您很重要(无论出于何种原因),您需要将值存储为字符串,然后使用parseFloat提取值。

For example: 例如:

var int = "5";
var float = "5.0";
console.log(int == float); // false
console.log(parseFloat(int) == parseFloat(float)); // true

How to print numbers to fixed number of decimal places 如何将数字打印到固定的小数位数

I've just spotted Zerstoren comment: "i get data from mongodb and want to print this data with strong typing." 我刚刚发现了Zerstoren的评论:“我从mongodb获取数据,并希望通过强类型打印这些数据。”

The method toFixed can be used to print a number to fixed number of decimal places: toFixed方法可用于将数字打印到固定数量的小数位:

var num = 5;
console.log( num.toFixed(1) ); // 5.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM