Anyone a suggestion on how to convert a string to a number in TypeScript?
有人有关于如何在打字稿中将字符串转换成数字的建议吗?
var aNumber : number = "1"; // --> Error
// Could this be done?
var defaultValue = 0;
var aNumber : number = "1".toInt32(defaultValue);
// Or ..
var defaultValue = 0;
var aNumber : number = StringToInt("1", defaultValue);
Update: I did some extra puzzling, the best sofar I've come up with: var aNumber : number = ( "1") * 1;
更新:我做了一些额外的令人困惑的工作,目前为止我所做的最好的工作:var aNumber: number =(“1”)* 1;
checking if a string is numeric is answered here: In Typescript, How to check if a string is Numeric.
检查字符串是否为数字的回答在这里:在Typescript中,如何检查字符串是否为数字。
7 个解决方案
#1
455
You can use the parseInt
or parseFloat
functions, or simply use the unary +
operator:
您可以使用parseInt或parseFloat函数,也可以使用一元+运算符:
var x = "32";
var y = +x; // y: number
#2
543
The Typescript way to do this would be:
打字稿的方式是:
Number('1234') // 1234
Number('9BX9') // NaN
as answered here: https://*.com/a/23440948/2083492
在这里回答:https://*.com/a/23440948/2083492
#3
40
For our fellow Angular users:
对于我们的角用户来说:
Within a template, Number(x)
and parseInt(x)
throws an error, and +x
has no effect. Valid casting will be x*1
or x/1
.
在模板中,Number(x)和parseInt(x)抛出一个错误,+x没有影响。有效施法为x*1或x/1。
#4
34
Expounding on what Ryan said, TypeScript embraces the JavaScript idioms in general.
在解释Ryan所说的内容时,打字稿大体上包含了JavaScript习惯用法。
var n = +"1"; // the unary + converts to number
var b = !!"2"; // the !! converts truthy to true, and falsy to false
var s = ""+3; // the ""+ converts to string via toString()
All the interesting in-depth details at JavaScript Type Conversion.
JavaScript类型转换的所有有趣的深入细节。
#5
18
As shown by other answers here, there are multiple ways to do the conversion:
如其他答案所示,转换有多种方式:
Number('123');
+'123';
parseInt('123');
parseFloat('123.45')
I'd like to mention one more thing on parseInt
though.
我想再提一件关于parseInt的事。
When using parseInt
, it makes sense to always pass the radix parameter. For decimal conversion, that is 10
. This is the default value for the parameter, which is why it can be omitted. For binary, it's a 2
and 16
for hexadecimal. Actually, any radix between and including 2 and 36 works.
使用parseInt时,总是传递基数参数是有意义的。对于十进制转换,这是10。这是参数的默认值,因此可以省略它。对于二进制,它是十六进制的2和16。实际上,2和36之间的任何基数都可以。
parseInt('123') // 123 (don't do this)
parseInt('123', 10) // 123 (much better)
parseInt('1101', 2) // 13
parseInt('0xfae3', 16) // 64227
The parseInt
function, well, parses strings to convert them to numbers. In some JS implementations, parseInt
parses leading zeros as octal:
parseInt函数解析字符串将其转换为数字。在一些JS实现中,parseInt将前导0解析为八进制:
Although discouraged by ECMAScript 3 and forbidden by ECMAScript 5, many implementations interpret a numeric string beginning with a leading 0 as octal. The following may have an octal result, or it may have a decimal result. Always specify a radix to avoid this unreliable behavior.
虽然ECMAScript 3不支持,ECMAScript 5也不允许,但是很多实现都将以0开头的数字字符串解释为八进制。以下可能有八进制结果,也可能有十进制结果。总是指定一个基数来避免这种不可靠的行为。
— MDN
——MDN
The fact that code gets clearer is a nice side effect of specifying the radix parameter.
代码变得更清晰是指定基数参数的一个很好的副作用。
Since parseFloat
only parses numeric expressions in radix 10, there's no need for a radix parameter here.
由于parseFloat只解析基数10中的数值表达式,所以这里不需要基数参数。
更多:
- Why do we need to use radix? (SO Question)
- 为什么我们需要使用基数?(这样的问题)
- SO Answer to "What is the difference between parseInt() and Number()?"
- 所以回答“parseInt()和Number()之间的区别是什么?”
#6
9
You can follow either of the following ways.
您可以采用以下任何一种方法。
var str = '54';
var num = +str; //easy way by using + operator
var num = parseInt(str); //by using the parseInt operation
#7
2
There are inbuilt functions like parseInt()
, parseFloat()
and Number()
in Typescript, you can use those.
在类型文件中有一些内置函数,如parseInt()、parseFloat()和Number(),您可以使用它们。
#1
455
You can use the parseInt
or parseFloat
functions, or simply use the unary +
operator:
您可以使用parseInt或parseFloat函数,也可以使用一元+运算符:
var x = "32";
var y = +x; // y: number
#2
543
The Typescript way to do this would be:
打字稿的方式是:
Number('1234') // 1234
Number('9BX9') // NaN
as answered here: https://*.com/a/23440948/2083492
在这里回答:https://*.com/a/23440948/2083492
#3
40
For our fellow Angular users:
对于我们的角用户来说:
Within a template, Number(x)
and parseInt(x)
throws an error, and +x
has no effect. Valid casting will be x*1
or x/1
.
在模板中,Number(x)和parseInt(x)抛出一个错误,+x没有影响。有效施法为x*1或x/1。
#4
34
Expounding on what Ryan said, TypeScript embraces the JavaScript idioms in general.
在解释Ryan所说的内容时,打字稿大体上包含了JavaScript习惯用法。
var n = +"1"; // the unary + converts to number
var b = !!"2"; // the !! converts truthy to true, and falsy to false
var s = ""+3; // the ""+ converts to string via toString()
All the interesting in-depth details at JavaScript Type Conversion.
JavaScript类型转换的所有有趣的深入细节。
#5
18
As shown by other answers here, there are multiple ways to do the conversion:
如其他答案所示,转换有多种方式:
Number('123');
+'123';
parseInt('123');
parseFloat('123.45')
I'd like to mention one more thing on parseInt
though.
我想再提一件关于parseInt的事。
When using parseInt
, it makes sense to always pass the radix parameter. For decimal conversion, that is 10
. This is the default value for the parameter, which is why it can be omitted. For binary, it's a 2
and 16
for hexadecimal. Actually, any radix between and including 2 and 36 works.
使用parseInt时,总是传递基数参数是有意义的。对于十进制转换,这是10。这是参数的默认值,因此可以省略它。对于二进制,它是十六进制的2和16。实际上,2和36之间的任何基数都可以。
parseInt('123') // 123 (don't do this)
parseInt('123', 10) // 123 (much better)
parseInt('1101', 2) // 13
parseInt('0xfae3', 16) // 64227
The parseInt
function, well, parses strings to convert them to numbers. In some JS implementations, parseInt
parses leading zeros as octal:
parseInt函数解析字符串将其转换为数字。在一些JS实现中,parseInt将前导0解析为八进制:
Although discouraged by ECMAScript 3 and forbidden by ECMAScript 5, many implementations interpret a numeric string beginning with a leading 0 as octal. The following may have an octal result, or it may have a decimal result. Always specify a radix to avoid this unreliable behavior.
虽然ECMAScript 3不支持,ECMAScript 5也不允许,但是很多实现都将以0开头的数字字符串解释为八进制。以下可能有八进制结果,也可能有十进制结果。总是指定一个基数来避免这种不可靠的行为。
— MDN
——MDN
The fact that code gets clearer is a nice side effect of specifying the radix parameter.
代码变得更清晰是指定基数参数的一个很好的副作用。
Since parseFloat
only parses numeric expressions in radix 10, there's no need for a radix parameter here.
由于parseFloat只解析基数10中的数值表达式,所以这里不需要基数参数。
更多:
- Why do we need to use radix? (SO Question)
- 为什么我们需要使用基数?(这样的问题)
- SO Answer to "What is the difference between parseInt() and Number()?"
- 所以回答“parseInt()和Number()之间的区别是什么?”
#6
9
You can follow either of the following ways.
您可以采用以下任何一种方法。
var str = '54';
var num = +str; //easy way by using + operator
var num = parseInt(str); //by using the parseInt operation
#7
2
There are inbuilt functions like parseInt()
, parseFloat()
and Number()
in Typescript, you can use those.
在类型文件中有一些内置函数,如parseInt()、parseFloat()和Number(),您可以使用它们。