typeescript:在数字上使用parseInt()时出错

[英]TypeScript: error when using parseInt() on a number


The JavaScript function parseInt can be used to force conversion of a given parameter to an integer, whether that parameter is a string, float number, number, etc.

JavaScript函数parseInt可用于强制将给定参数转换为整数,无论该参数是字符串、浮点数、数字等等。

In JavaScript, parseInt(1.2) would yield 1 with no errors, however, in TypeScript, it throws an error during compilation saying:

在JavaScript中,parseInt(1.2)将产生1个无错误,但是在TypeScript中,它在编译过程中抛出一个错误,说:

error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.

Am I missing something here or is it an expected behaviour from TypeScript?

我是不是漏掉了什么东西,或者是打字稿的预期行为?

6 个解决方案

#1


16  

Don't use parseInt to do this operation -- use Math.floor.

不要使用parseInt来执行此操作——使用Math.floor。

Using parseInt to floor a number is not always going to yield correct results. parseInt(4e21) returns 4, not 4e21. parseInt(-0) returns 0, not -0.

使用parseInt到层数并不总是会产生正确的结果。parseInt(4e21)返回4,而不是4e21。parseInt(-0)返回0,而不是-0。

#2


4  

The function parseInt indeed expects a string in its first argument. Please check the documentation. Usually you can omit the second, radix argument and then it will fall back to the default of 10. But the safest is to always add the numeric system base as second argument (usually 10).

函数parseInt确实期望在第一个参数中有一个字符串。请检查文档。通常你可以省略第二个参数,基数参数,然后它会回到默认值10。但是最安全的方法是始终将数字系统基作为第二个参数(通常是10)。

If you'd like to cast a general value to number, you can use the Number function, like this.

如果您想将一个通用值转换为number,可以使用number函数,如下所示。

var myNumber = Number(myGeneralValue);

#3


3  

Look at the typing:

看看输入:

  parseInt(string: string, radix?: number): number;
                   ^^^^^^

The first argument needs to be a string. That's in line with the spec:

第一个参数必须是字符串。这符合规范:

parseInt (string , radix)
The parseInt function produces an integer value dictated by interpretation of the contents of the string argument according to the specified radix.

parseInt (string, radix) parseInt函数生成一个整数值,该整数值由根据指定的基数对字符串参数内容的解释决定。

In normal JS, the first argument is coerced to a string, based on the following rule in the spec:

在普通JS中,第一个参数被强制为字符串,基于规范中的以下规则:

  1. Let inputString be ToString(string).
  2. 让inputString ToString(字符串)。

which is why parseInt(1.2) works.

这就是parseInt(1.2)起作用的原因。

Note that the spec allows radix to be undefined, which is the same as omitting it, hence the question mark in the radix?: number part of the signature. In this case, of course, it defaults to 10 (unless the string looks like 0xabc).

注意,该规范允许未定义基数,这与省略它相同,因此在基数上有问号?:签名部分编号。当然,在这种情况下,它默认为10(除非字符串看起来像0xabc)。

As mentioned in other answers, parseInt is not the best solution anyway if what you really want to do is a floor or truncation operation.

正如在其他答案中提到的,无论如何,如果您真正想要做的是一个楼层或截断操作,parseInt并不是最好的解决方案。

#4


2  

Why would you use parseInt in this case? Just use Math.floor or Math.ceil. parseInt expects a string as an argument and not a number. Hence your error

为什么要在这种情况下使用parseInt ?用数学。楼或Math.ceil。parseInt希望字符串作为参数,而不是数字。因此你的错误

#5


1  

There are different manifestations when negative numbers between 'Math.floor' and 'parseInt'.

在“数学”之间的负数有不同的表现形式。楼”和“方法”。

you should use this: 1.2 | 0

您应该使用:1.2 | 0。

or (1.2).toFixed(0)

或(1.2).toFixed(0)

#6


0  

I think other people have already given lots of valid answers here, but in my opinion the easiest approach would be to call .toString() on the original value, and to explicit the radix:

我认为其他人已经给出了很多有效的答案,但是在我看来,最简单的方法是对原始值调用.toString(),并显式使用基数:

parseInt((1.2).toString(), 10);

方法((1.2).toString(),10);


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/09/13/4f546951f669ddcca3502431974ed6f8.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com