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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:14:59  来源:igfitidea点击:

How to convert a String to long in javascript?

javascriptstringlong-integerunix-timestamp

提问by Richard H

I have a millisecond timestamp that I need to convert from a String to long. Javascript has a parseIntbut 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 Numbertype which is a 64 bit floating point number*.

JavaScript 的Number类型是 64 位浮点数*。

If you're looking to convert a string to a number, use

如果您想将字符串转换为数字,请使用

  1. either parseIntor parseFloat. If using parseInt, I'd recommend always passing the radix too.
  2. use the Unary +operator e.g. +"123456"
  3. use the Numberconstructor e.g. var n = Number("12343")
  1. 无论是parseIntparseFloat。如果使用parseInt,我建议也总是传递基数。
  2. 使用一元+运算符,例如+"123456"
  3. 使用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 longin javascript.

这是因为longjavascript 中没有。

http://javascript.about.com/od/reference/g/rlong.htm

http://javascript.about.com/od/reference/g/rlong.htm