简体   繁体   English

为什么在JavaScript中将“ 1.0”传递给函数将其更改为“ 1”

[英]Why does passing '1.0' to a function change it to '1' in javascript

Can someone explain to me why when pass 1.0 to a function in javascript it gets converted to 1 and how to work around this quirk? 有人可以向我解释为什么将1.0传递给javascript中的函数时将其转换为1,以及如何解决这一问题?

var return_me = function(value) {
    return value;
}

console.log("1.0 is returned as " + return_me(1.0));

JavaScript doesn't distinguish between int or float like other more strongly typed languages. JavaScript不会像其他更强类型的语言一样区分intfloat It just has one Number type. 它只有一种Number类型。 From the ECMA specifications : 根据ECMA规范

Once the exact mathematical value (MV) for a numeric literal has been determined, it is then rounded to a value of the Number type. 一旦确定了数字文字的精确数学值(MV),就将其四舍五入为Number类型的值。 If the MV is 0, then the rounded value is +0; 如果MV为0,则舍入值为+0; otherwise, the rounded value must be the Number value for the MV (as specified in 8.5), unless the literal is a DecimalLiteral and the literal has more than 20 significant digits, in which case the Number value may be either the Number value for the MV of a literal produced by replacing each significant digit after the 20th with a 0 digit or the Number value for the MV of a literal produced by replacing each significant digit after the 20th with a 0 digit and then incrementing the literal at the 20th significant digit position. 否则,四舍五入的值必须是MV的Number值(如8.5中所指定),除非文字是DecimalLiteral且文字具有20个以上的有效数字,在这种情况下,Number值可以是通过将20位之后的每个有效数字都替换为0位而产生的文字的MV,或者将20位之后的每个有效数字都替换为0位并随后将其第20个有效数字递增的文字所产生的MV的Number值。位置。 A digit is significant if it is not part of an ExponentPart and 如果数字不是ExpnentPart的一部分,则该数字是有效的;并且

  • it is not 0; 不为0;

  • or there is a nonzero digit to its left and there is a nonzero digit, not in the ExponentPart, to its right. 或左侧有一个非零数字,右侧有一个非零数字(不在ExponentPart中)。

A conforming implementation, when processing strict mode code (see 10.1.1), must not extend the syntax of NumericLiteral to include OctalIntegerLiteral as described in B.1.1. 在处理严格模式代码(请参见10.1.1)时,符合标准的实现不得将B.1.1中所述的NumericLiteral语法扩展为包括OctalIntegerLiteral。

More info on Number . 有关Number更多信息。

So basically, the answer is that JavaScript will display numbers that look like integers as integers and numbers that look like floats as floats. 因此,基本上,答案是JavaScript将把看起来像整数的数字显示为整数,将像浮点数的数字显示为浮点数。

In javascript there are six build in types of values. 在javascript中,有六种内置值类型。

  1. string
  2. number
  3. boolean 布尔
  4. null and undefined 空和未定义
  5. object 宾语
  6. symbol 符号

These are mentioned to the book You don't know JS which I find really useful in my effort to learn javascript. 您不认识JS的书中提到了这些内容,我发现JS对学习javascript确实很有用。

As a result js sees the var value of your function as a typeof number and understands that 1.0 is the same as 1. (in case the 1.0 was 1.9 it returns 1.9 as expected). 结果,js将函数的var值视为typeof数字,并理解1.0与1相同(如果1.0为1.9,则按预期返回1.9)。

Now if you want to keep these decimals (even if there are zero digits) you could pass the value as a string. 现在,如果要保留这些小数(即使有零位数字),也可以将值作为字符串传递。

console.log("1.0 is returned as " + return_me("1.0"));

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

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