How to compare locale dependent values?

I need to compare the float value entered in a web form in a range. The problem is that client computers can have different locale settings, which means that the user can use either "." or "," to separate the integer part from the decimal.

Is there an easy way to do this? As for the intranet, and that they are only allowed to use IE, VBScript is fine, even if I prefer to use JavaScript.

EDIT: Let me clarify this a bit:

I can’t rely on the locale of the system, because, for example, many of our French customers use a computer with the English locale, even if they still use a comma to fill in data on web forms.
Thus, I need a way to check for multiple language versions of "string to double".
I know that the rise condition is “digits about digits with three decimal digits”, but in our environment such an answer never happens, and if this happens, it will be caused by an error out of range due to multiplication by thousands, so this is not real problem for us.

+4
source share
5 answers

In Javascript, use parseFloat for the text value to get the number. Similarly in VBScript, use the text value CDbl . Both must match the current locale settings for the user.

+1
source

This code should work:

  function toFloat(localFloatStr) var x = localFloatStr.split(/,|\./), x2 = x[x.length-1], x3 = x.join('').replace(new RegExp(x2+'$'),'.'+x2); return parseFloat(x3); // x2 is for clarity, could be omitted: //=>x.join('').replace(new RegExp(x[x.length-1]+'$'),'.'+x[x.length-1]) } alert(toFloat('1,223,455.223')); //=> 1223455.223 alert(toFloat('1.223.455,223')); //=> 1223455.223 // your numbers ;~) alert(toFloat('3.123,56')); //=> 3123.56 alert(toFloat('3,123.56')); //=> 3123.56 
+1
source

What we do, try parsing using the user's culture, and if that doesn't work, analyze it using an invariant culture.

I would not know how to do this in javascript or vbscript for sure.

0
source

I used the KooiInc answer, but changed it a bit because it did not take into account some cases.

 function toFloat(strNum) { var full = strNum.split(/[.,]/); if (full.length == 1) return parseFloat(strNum); var back = full[full.length - 1]; var result = full.join('').replace(new RegExp(back + '$'), '.' + back); return parseFloat(result); } 
0
source

Prohibit the use of any thousands separator.

Give an example to the user: "Reals should look like this: 3123.56 or 3123,56 ." Then just change to . and analyze it.

You can always tell the user that he did something wrong with this message:

 "I don't understand what you mean by "**,**,**". Please format numbers like "3123.56." 
-1
source

All Articles