1

I am attempting to develop a conversion website that takes a numeric value:

 1,200.12
 or
 1.200,12
 or
 1200.12
 or
 1200,12
 and have them all interpreted as 1200.12 by parseFloat.

 I would also like decimals to be able to be interpreted.
 0.123
 or
 0,123     
 as 0.123

through a textarea and then parseFloat the number in order to perform calculations. These are the results I am getting:

textarea input = 12,000.12
value after parseFloat = 12

Does parseFloat not recognize the formatting of the numbers? i get the same results with:

textarea input: 12.000,12
value after parseFloat = 12

How do I solve this problem? It would seem I need to strip out the commas since parseFloat doesn't read beyond them and with european notation strip the decimals and change the comma to a decimal for parseFloat to read the input correctly. Any ideas on how to solve this? My guess is I would need to identify the string input as either european or american decimal notation and then perform the required actions to prepare the string for parseFloat. How would I go about achieving that? All contributions are appreciated. Using HTML5 and Javascript. This is my first website so please go easy on me.

Best, RP

To all contributors...Thank you! So far all the input has been sweet. I don't think we are going to be able to use a single replace statement to correctly strip both european and american notation so I think I should use REGEX somehow to determine the notation and then split into an if else statement to perform separate replace functions on each individual notation.

var input, trim;
input = "1.234,56"   //string from textarea on page
if(/REGEX that determines American Notation/.test(input){ 
   trim =  input.replace(/\,/,"");//removes commas and leaves decimal point);
}
else(/REGEX that determine European Notation/.test(input)/){ //would qualify input here
   rep = input.replace(/\./,"");//removes all decimal points);
   trim = rep.replace(/\,/,"."//changes the remaining comma to a decimal);
}
//now either notation should be in the appropriate form to parse
number = parseFloat(trim);

Is this possible using REGEX? Please see my other question. Regex - creating an input/textarea that correctly interprets numbers

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Is "1,234" intended to be an integer with 4 digits, or a decimal? – recursive Aug 21 '14 at 21:54
  • Is it known to be always 2 decimals? If so you're able to slice the end of the string, strip the rest and from there parseFloat it – Erik Svedin Aug 21 '14 at 22:03
  • @recursive either or. I want to beable to take numbers entered in any format 1,234 1234 1.234(EUnotation) and prepare them for parseFloat. – Ryan Pace Sloan Aug 21 '14 at 22:09
  • @Svedin i want them to be able to enter up to 5 decimal places. This is for converting lengths of measure from metric to english standard – Ryan Pace Sloan Aug 21 '14 at 22:09
  • @RyanPaceSloan: But *how* do you want the number to be prepared? If a user inputs "1,234", what is the correct output of the program? Would you consider the integer correct, or the decimal? Or would the program be allowed to pick whichever it preferred? – recursive Aug 21 '14 at 22:16
  • @recursive the correct interpretation would be for 1,234.56 and 1.234,56 and 1234.56 and 1234,56 to ALL be interpreted as 1234.56. – Ryan Pace Sloan Aug 21 '14 at 22:31
  • The question in the title is very different from what is asked at the end of the text, which in turn is very vague. There are several number notations in use in Europe. It is impossible to write code that accepts all the different notations “correctly”, since “correct” depends on the conventions (e.g. 1.500 means one and a half in some cultures, one thousand five hundred in some others, and is invalid data in some). So it is unclear what is being asked here. – Jukka K. Korpela Aug 22 '14 at 05:00
  • @Jukka I just decided to go with a single format N,NNN.NN as I realized the ambiguity of trying to interpret what notation someone is using without telling the computer which one to interpret it with. – Ryan Pace Sloan Aug 22 '14 at 14:06

3 Answers3

5

One way would be to strip the comma signs, for example with:

.replace(",", "")

From there you should be able to parseFloat

Updated with fiddle: http://jsfiddle.net/aLv74xpu/2/

Erik Svedin
  • 1,286
  • 12
  • 26
  • That still has problems with input like 1.234.567 It would work if you replaced all periods except one. – recursive Aug 21 '14 at 22:14
  • This would work for american notation 1,234.00 as the decimal would be left behind for parseFloat to interpret. However if european notation were used 1.234,00 it would replace the decimal point and leave the first period. parseFloat would interpret 1234 as 1.00 based on what gillesc said. – Ryan Pace Sloan Aug 21 '14 at 22:29
  • I see, I probably misread the question or it wasnt fully updated by the time i answered it. If you're sure there is a decimal value @recursive posted a nice answer with the regex – Erik Svedin Aug 21 '14 at 22:31
3

Here is a solution that uses a regular expression to eliminate all commas and all periods, except the last one.

var number = "1,234.567.890";
var replaced = number.replace(/,|\.(?=.*\.)/g, "");
var result = parseFloat(replaced);
// result === 1234567.89

Alternatively, you can use this, which treats commas and periods identically, and ignores them all except for the last one.

var number = "12.345,67";
var replaced = number.replace(/[.,](?=.*[.,])/g, "").replace(",", ".");
var result = parseFloat(replaced);
// result === 12345.67
recursive
  • 83,943
  • 34
  • 151
  • 241
  • what if the last period is a comma? ie 1.234,56 will it change that to a decimal? – Ryan Pace Sloan Aug 21 '14 at 22:32
  • This is great! It works perfectly on american notation. But with European it interprets 12.345,67 as 12.34567 instead of 12345.67. Any ideas? Thanks for the contribution to the cause! – Ryan Pace Sloan Aug 21 '14 at 22:40
  • @RyanPaceSloan: I think I understand better what you want. I posted an alternative solution that may work. – recursive Aug 21 '14 at 23:01
  • AMAZING! Thank you so much. It worked like a charm. I gave it 12,000,000,000.1234567 and it returned 12000000000.1234567 also 12.000.000.000,1234567 and same result as american. I really can't thank you enough! You are awesome! – Ryan Pace Sloan Aug 21 '14 at 23:12
  • what if someone enters 12,000 without a decimal point? it will then read it as 12.00. Darn it! I thought we had this licked! Any more magic? – Ryan Pace Sloan Aug 21 '14 at 23:33
  • I didn't specify that the text field would also be receiving non decimal input as well. 12000 12,000 12.000...all translate to 12000 – Ryan Pace Sloan Aug 21 '14 at 23:46
  • This brings me back to my if else statement idea – Ryan Pace Sloan Aug 21 '14 at 23:47
  • How can you tell if 12.000 should result in 12000 or 12? It seems ambiguous. My original question still stands. What is the correct output? You can't say "both", because it's a computer program and needs to have specified behavior. If you can decide what you want it to do, there's probably a solution. – recursive Aug 22 '14 at 00:08
  • Now I understand why you were asking that. This is a follow up to our question-> http://stackoverflow.com/questions/25437910/how-to-make-a-regex-statement-that-will-allow-interpretation-of-eu-and-us-decima – Ryan Pace Sloan Aug 22 '14 at 00:28
  • Specifically I want thousand separator input 12,000 & 12.000 and non seperator input 12000 all to be 12 thousand. And also decimal input 12.345 & 12,345 to be 12 point 345 and not 12 thousand 345. Is is even possible? How can REGEX tell the difference between 12,000 (12 thousand in US Notation) and 12,123 (12 point 123 in EU Notation)? Perhaps I should just pick a format. Or perhaps an if else statement based on the structure of the notation. – Ryan Pace Sloan Aug 22 '14 at 00:33
  • @RyanPaceSloan: You first must decide what you want the behavior to be. If the correct result for `"12,000"` is 12000 and for `"12,123"` is 12.123, there is very little hope of a totally automated solution without using some other source of information or mind-reading. – recursive Aug 22 '14 at 00:35
1

parseFloat parses its argument, a string, and returns a floating point number. If it encounters a character other than a sign (+ or -), numeral (0-9), a decimal point, or an exponent, it returns the value up to that point and ignores that character and all succeeding characters. Leading and trailing spaces are allowed.

From the good MDN network: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat

So it is the expected behaviour of parseFloat

GillesC
  • 10,647
  • 3
  • 40
  • 55