I have defined an input in HTML that represents a number. I need to parse the string in JavaScript to a number taking into consideration the different languages that will be entered by the user, for example: '1.34' in English will be written as '1,34' in French. parseFloat('1,344')
will be return 1 in case we are in English standard.
-
You need to use a library that does the formatting. One such library is [Globalize](https://github.com/jquery/globalize) but there are others, too. – VLAZ Aug 20 '16 at 12:21
-
1what you have tried so far?? – Shakir Ahamed Aug 20 '16 at 12:37
-
[You are not the only who want to do this](http://stackoverflow.com/questions/25645163/to-convert-string-to-number-according-to-locale-opposite-of-tolocalestring) – Denis Tambovchanin Aug 20 '16 at 12:41
2 Answers
You could probably find a library for it, but you can also pretty easily format the numbers into the wanted format yourself.
When you get a number from the input just convert it to string and then use the indexOf() function (http://www.w3schools.com/jsref/jsref_indexof.asp) to see if there's a comma or a dot in the number. It returns the position index of that element in a string so you can then replace with the wanted one to format the number. Position will be -1 if there is no dot/comma.
var num = 32.14;
var string = String(num);
var position = string.indexOf(".");
Hope this helps you.

- 198
- 1
- 11
If it's only those two representations you consider, then another easy solution is to always do
var floatNum = num.replace(/,/g,".");
and then just treat it like any float number.
Unless you really need it for other number systems I'd avoid using a library. Libraries tend to be too big for most projects to utilize properly in my opinion.

- 33
- 6