1

I have been working on a tough question and gotten a lot of help from programmers and developers on this site. Many thanks to those who contribute. You are invaluable.

Using Javascript and HTML. I am a newbie.

The current condition of my problem is this...

I am attempting to develop a metric to english conversion website that takes a numeric value in a HTML textbox (no form):

I want the user to have multiple ways of entering the data into the conversion tool as it will be used internationally. So...

1,200 or 1.200 or 1200 should all be interpreted by parseFloat as 1200.00

1,200.12 or 1.200,12 or 1200.12 or 1200,12 should all be interpreted as 1200.12.

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

I realize that I need to strip the thousand seperators out (whether comma or period) and set the final decimal to a period if it is a comma. A developer on this site gave me a REGEX statement that I could use in conjunction with a javascript replace statement to handle this.

trim = trim.replace(/[.,](?=.*[.,])/g, "").replace(",", "."); 
//care of recursive on stack overflow

This seemed to solved the problem with one exception. Anytime data is entered using a thousand separator without using a decimal point this code interprets the separator as a decimal and the value goes from 12,345 to 12.345 or (EU Notation as well). Assuming that the user will use commas but no decimals, this is a troublesome problem as the results will not be correct. How am I to remedy this? Is there an adjustment I can make to the REGEX to have it determine when a comma is a thousand separator (1.234) or a decimal (1,234), or when a period is a thousand separator (1,234) or a decimal point (1.234)?

Thanks in Advance,

RP

  • How will **you** determine what `1,885` is? – PM 77-1 Aug 22 '14 at 00:28
  • No, you can't. `1.234 ` and `1,234` mean different things in different places. Then, you must ask the user which decimal separator is using, or tell him which one should use. – Oriol Aug 22 '14 at 00:29
  • One of your examples states that `"1,200"` should yield `1200`, but another states `"0,123"` should yield `0.123`. What's the rule for determining the difference? – recursive Aug 22 '14 at 00:31
  • You guys hit the problem right on the head. I think the solution is to choose a single format and dictate to the user how to enter the data. – Ryan Pace Sloan Aug 22 '14 at 00:36
  • Or to have the user specify which notation they want to use and have the computer apply the appropriate interpretation. Look what humans can do that computers can't! Were still smarter :o)! – Ryan Pace Sloan Aug 22 '14 at 00:39
  • I think you will need to use the locale of the users browser [see this question for more information](http://stackoverflow.com/questions/297542/simplest-way-to-detect-client-locale-in-php) – RiggsFolly Jan 23 '16 at 00:48

0 Answers0