28

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

Code:

var num = "82144251";

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

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
Rubén
  • 34,714
  • 9
  • 70
  • 166
Valip
  • 4,440
  • 19
  • 79
  • 150
  • 5
    The `.toFixed()` function does, in fact, return a string, not a number. – Pointy Nov 18 '16 at 20:39
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed – Scott Marcus Nov 18 '16 at 20:39
  • 1
    you're looking for `Number(num)` – georg Nov 18 '16 at 20:40
  • 2
    Also note that `8.2144251E7` is precisely the same value as `82144251`. – Pointy Nov 18 '16 at 20:40
  • @georg the same issue with Number(num) – Valip Nov 18 '16 at 20:41
  • @ScottMarcus I tried with .toFixed(0), it still returns a string – Valip Nov 18 '16 at 20:41
  • @Pointy I need it to be exactly `82144251`, without any decimals – Valip Nov 18 '16 at 20:42
  • 3
    @PavelValeriu Why are you calling `.toFixed()` at all? `parseInt()` returns a number, that's all you need. – Barmar Nov 18 '16 at 20:42
  • 1
    @PavelValeriu That's because `.toFixed()` always returns a string as the documentation I provided explains. See my answer below for a fix. – Scott Marcus Nov 18 '16 at 20:42
  • 2
    I'm confused why the result of `parseInt` isn't what you're after. –  Nov 18 '16 at 20:42
  • 3
    @PavelValeriu: what issue? "parseInt(num) returns 8.2144251E7"? - no it doesn't – georg Nov 18 '16 at 20:43
  • @PavelValeriu `8.2144251E7` **is** exactly `82144251`. They are two ways of writing the same numeric value. Perform an `===` comparison if you don't believe me. When you `alert()` or `console.log()` a number, it gets converted back to a string for display purposes. – Pointy Nov 18 '16 at 20:54
  • @PavelValeriu: `num =+ "num";` is the problem. What is this line supposed to do? – georg Nov 18 '16 at 20:56
  • @PavelValeriu: ok, I think it's `Logger.log` that corrupts your data. Try inserting `num` in the document, it should be all right – georg Nov 18 '16 at 21:00
  • @georg I'll check it, thanks! – Valip Nov 18 '16 at 21:07
  • Possible duplicate of [Javascript: Converting String to Number?](http://stackoverflow.com/questions/2843897/javascript-converting-string-to-number) – Rubén Nov 19 '16 at 14:27

6 Answers6

35

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

check these snippets

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));

Hope it helps

Aaron Thoma
  • 3,820
  • 1
  • 37
  • 34
Geeky
  • 7,420
  • 2
  • 24
  • 50
11

It looks like you're looking for the Number() functionality here:

var num = "82144251"; // "82144251"
var numAsNumber = Number(num); // prints 82144251
typeof num // string
typeof numAsNumber // number

You can read more about Number() here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number

Hope this helps!

Dave Cooper
  • 10,494
  • 4
  • 30
  • 50
  • 2
    It is widely considered a bad practice to use the `Number()`, `String()` and `Boolean()` constructor functions. – Scott Marcus Nov 18 '16 at 20:48
  • 1
    Hey @ScottMarcus thanks for the heads up on that. We learn something new every day :) Would you be able to help me understand why that is? – Dave Cooper Nov 18 '16 at 20:55
  • @ScottMarcus why is `Number()` a bad practice? `parseInt` and `Number` have different purpose. If I want to accept `100px` or similar as input then `parseInt` will be useful, if I want to ensure that the string only contains numbers (including trailing and leading spaces, `e` notation and hex) then `Number()` would give me the correct result. – t.niese Nov 18 '16 at 20:56
  • @Dave Cooper Because they are a bit of an anti-pattern. They are constructors that you don't have to use with `new` and we have other more intuitive means to do the jobs. – Scott Marcus Nov 18 '16 at 20:57
  • @ScottMarcus so you'd say parseInt is a more sane solution? If so, why? – Dave Cooper Nov 18 '16 at 21:02
  • 2
    @DaveCooper `Number()` creates a number object behind the scenes, rather than just sticking with a type, which (depending on implementation) incurs more overhead and is easily confused with `new Number()`. This is true of `String()` and `Boolean()` as well. – Scott Marcus Nov 18 '16 at 21:06
5

No questions are dumb.

a quick answer:

to convert a string to a number you can use the unary plus.

var num = "82144251";

num = +num;

Doing num = +num is practically the same as doing num = num * 1; it converts the value in a to a number if needed, but after that it doesn't change the value.

Chris Haugen
  • 825
  • 1
  • 7
  • 22
  • So what happens when the string contains a non-numeric character? – Scott Marcus Nov 18 '16 at 20:47
  • It should evaluate to NAN, however, your question does not specify it needs to handle non numbers. Just that `num = parseInt(num).toFixed()` returns a string with an E in it. – Chris Haugen Nov 18 '16 at 20:50
0

var num = "82144251";
num = parseInt(num).toFixed()
console.log(num, typeof num); // string
num = parseFloat(num);
console.log(num, typeof num); // number
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
0

var intNum = parseInt("82144251", 10); // intNum is number

-1

I use num-0, since it is easier for inline use.

selytch
  • 535
  • 2
  • 9
  • 24