如何在JavaScript中将String转换为long? [英] How to convert a String to long in javascript?

查看:2546
本文介绍了如何在JavaScript中将String转换为long?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个毫秒时间戳,我需要从String转换为long。 Javascript有 parseInt 但不是 parseLong 。那么我该怎么办?

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?

谢谢

编辑:要展开我的问题有点:鉴于显然javascript没有long类型,我怎样才能用最初表示为字符串的long进行简单的算术运算?例如,从另一个中减去一个以获得时间增量?

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有一个数字 type是64位浮点数*。

JavaScript has a Number type which is a 64 bit floating point number*.

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

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


  1. parseInt parseFloat 。如果使用 parseInt ,我建议也一直传递基数。

  2. 使用一元 + 运营商例如 +123456

  3. 使用 Number 构造函数,例如 var n = Number(12343)

  1. either parseInt or parseFloat. If using parseInt, I'd recommend always passing the radix too.
  2. use the Unary + operator e.g. +"123456"
  3. use the Number constructor e.g. var n = Number("12343")

* 那里是数字将在内部保持为整数的情况。

这篇关于如何在JavaScript中将String转换为long?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆