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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39475166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:50:38  来源:igfitidea点击:

TypeScript: error when using parseInt() on a number

javascripttypescript

提问by benjaminz

The JavaScript function parseIntcan 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 1with 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?

我在这里遗漏了什么还是 TypeScript 的预期行为?

回答by Ryan Cavanaugh

Don't use parseIntto do this operation -- use Math.floor.

不要parseInt用来做这个操作——使用Math.floor.

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

使用parseIntfloor一些并不总是会产生正确的结果。parseInt(4e21)返回4,不是4e21parseInt(-0)返回0,不是-0

回答by Zoltán Tamási

The function parseIntindeed expects a stringin its first argument. Please check the documentation. Usually you can omit the second, radixargument 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确实string在其第一个参数中期望 a 。请检查文档。通常你可以省略第二个radix参数,然后它会回退到默认值10。但最安全的是始终将数字系统基数添加为第二个参数(通常为10)。

If you'd like to casta generalvalue to number, you can use the Numberfunction, like this.

如果你想一个一般的价值number,你可以使用Number功能,像这样。

var myNumber = Number(myGeneralValue);

回答by Zoltán Tamási

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 parseIntfunction produces an integer value dictated by interpretation of the contents of the stringargument 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 inputStringbe ToString(string).
  1. inputString为 ToString( string)。

which is why parseInt(1.2)works.

这就是为什么parseInt(1.2)有效。

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

请注意,规范允许基数undefined,这与省略它相同,因此radix?: number签名部分中的问号。在这种情况下,当然,它默认为 10(除非字符串看起来像0xabc)。

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

正如其他答案中提到的,parseInt如果您真正想做的是地板或截断操作,无论如何都不是最佳解决方案。

回答by Nahush Farkande

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

为什么要在这种情况下使用 parseInt?只需使用Math.floorMath.ceilparseInt需要一个字符串作为参数而不是数字。因此你的错误

回答by Kinnicore

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

'Math.floor' 和 'parseInt' 之间的负数有不同的表现。

you should use this: 1.2 | 0

你应该使用这个: 1.2 | 0

or (1.2).toFixed(0)

或 (1.2).toFixed(0)

回答by Giorgio Tempesta

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);

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

回答by cheeroke

A bit old but to put another way in to the pot:

有点旧,但换一种方式放入锅中:

Math.trunc();

Math.trunc();

see here for details.

有关详细信息,请参见此处