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