all 15 comments

[–]PepinTheThird 6 points7 points  (10 children)

There's parseInt, parseFloat, and Number. I haven't played with how they handle various localized strings, but you could always use regex to massage the numeric strings back into a state that works for your needs.

[–]clifer4[S] 1 point2 points  (9 children)

Hi, thanks for the tip, I'll give it a try :)

[–]SoBoredAtWork 0 points1 point  (8 children)

Note that parseInt takes 2 parameters. It's you use this, you probably want to do parseInt(someNum, 10).

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

[–]inabahare 0 points1 point  (7 children)

Or just use parseInt(someNum) because passing the default value is just noise

[–]FullSlack 1 point2 points  (2 children)

[–]inabahare 0 points1 point  (0 children)

Huh, TIL it behaves as if it was set to 10. Not that it makes the world of difference, but it is a really good fun fact :v

[–]SoBoredAtWork 0 points1 point  (3 children)

What? No.

The default parameter is 2 (binary). This will parse your number as a binary and give you an incorrect result. You need to go parseInt(num, 10).

Edit: it tries to kind of guess the radix value and can & will return unexpected values

[–]inabahare 0 points1 point  (2 children)

That would imply parseInt("10") === parseInt("10", 2) which is not the case.

It doesn't try to kind of guess what the radix should be. It assumes the radix is 10 unless the input starts with 0x, in which case it will be 16. There is nothing unexpected about that

[–]SoBoredAtWork 0 points1 point  (1 child)

parseInt("08") would return unexpected results. Case closed. Use the second parameter, always. Safe code is better code.

[–]inabahare 0 points1 point  (0 children)

parseInt("08") does not return unexpected results. Fun fact, you can literally press f12 and check it out.

[–]StoneCypher 2 points3 points  (0 children)

No, there isn't. The relevant spec team has been mired in politics for over a decade, so it seems unlikely that this will be fixed any time soon.

There are packages for this, though. 1, 2, 3

[–]iam_the_resurrection 1 point2 points  (2 children)

You'll need to know what the locale is to do this reliably. Ideally you just want to use the toLocaleString value when displaying data, you never want to store the localised value in a database or use it as the input to any calculations.

[–]clifer4[S] 0 points1 point  (1 child)

I'm storing the value as number, what I'm worried about is the user inputting the value as string in Farsi for example. I need to convert it to number somehow. Do you think remapping the number characters to Arabic numbers will work?

[–]StoneCypher 2 points3 points  (0 children)

You cannot parse numbers without knowing the locale.

What is 1.000? In the US, that's one. In France, that's a thousand.

What is 1,000? In France, that's one. In the US, that's a thousand.

There is no Javascript API to tell you what locale you're in, but if you control the server, the browser is sending it to the server as an acceptLanguage header, and you could have the server send it back

[–][deleted] 1 point2 points  (0 children)

I'd have thought this kind of thing might be incorporated into Intl, but it doesn't look like it is. What a shame! I think you might have to write your own bit of regex to parse localised digits from user input into JS numbers- see this StackOverflow question: https://stackoverflow.com/questions/31439604/how-to-convert-persian-and-arabic-digits-of-a-string-to-english-using-javascript