Javascript 如何在javascript中将String转换为long?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5450012/
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
How to convert a String to long in javascript?
提问by Richard H
I have a millisecond timestamp that I need to convert from a String to long. Javascript has a parseInt
but not a parseLong
. So how can I do this?
我有一个毫秒时间戳,我需要将它从字符串转换为 long。Javascript 有一个parseInt
但没有parseLong
. 那么我该怎么做呢?
Thanks
谢谢
Edit: To expand on my question slightly: given that apparently javascript doesn't have a long type, how can I do simple arithmetic with longs that are initially expressed as strings? E.g subtract one from the other to get a time delta?
编辑:稍微扩展一下我的问题:鉴于javascript显然没有long类型,我如何对最初表示为字符串的long进行简单的算术运算?例如,从另一个中减去一个以获得时间增量?
回答by Russ Cam
JavaScript has a Number
type which is a 64 bit floating point number*.
JavaScript 的Number
类型是 64 位浮点数*。
If you're looking to convert a string to a number, use
如果您想将字符串转换为数字,请使用
- either
parseInt
orparseFloat
. If usingparseInt
, I'd recommend always passing the radix too. - use the Unary
+
operator e.g.+"123456"
- use the
Number
constructor e.g.var n = Number("12343")
- 无论是
parseInt
或parseFloat
。如果使用parseInt
,我建议也总是传递基数。 - 使用一元
+
运算符,例如+"123456"
- 使用
Number
构造函数,例如var n = Number("12343")
*there are situations where the number will internally be held as an integer.
*在某些情况下,数字将在内部保留为整数。
回答by Jakub Konecki
It's because there is no long
in javascript.
这是因为long
javascript 中没有。