如何将字符串转换为数字 [英] How to convert a string to number

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

问题描述

我不知道如何将字符串82144251转换为数字.

I can't figure it out how to convert this string 82144251 to a number.

代码:

var num = "82144251";

如果我尝试使用.toFixed()函数下面的代码将我的数字转换回字符串...

If I try the code below the .toFixed() function converts my number back to a string...

问题更新: 我正在使用Google Apps脚本编辑器,那一定是问题所在...

Question update: I'm using the Google Apps Script editor and that must be the issue...

num = parseInt(num).toFixed() // if I just do parseInt(num) it returns 8.2144251E7

推荐答案

您可以使用一元运算符'+'或parseInt(number,10)或Number()将字符串转换为数字

You can convert a string to number using unary operator '+' or parseInt(number,10) or Number()

检查这些代码段

var num1a = "1";
console.log(+num1a);

var num1b = "2";
num1b=+num1b;
console.log(num1b);


var num3 = "3"
console.log(parseInt(num3,10));


var num4 = "4";
console.log(Number(num4));

希望有帮助

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

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