views:

789

answers:

4

I need to compare a float value entered in a web form against a range. The problem is that the client computers may have various locale settings, meaning that user may use either "." or "," to separate the integer part from decimal one.

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

EDIT: Let me clarify it a bit:

I cannot rely on the system locale, because, for example, a lot of our french customers use a computer with an english locale, even if they still use the comma to fill data in the web forms.
So I need a way to perform a check accross multiple locale "string to double" conversion.
I know that the raise condition is "what about numbers with 3 decimal digits", but in our environment, this kind of answer never happen, and if it happens, it will be threated as an out of range error due to the multiplication by a thousand, so it's not a real issue for us.

+2  A: 

In Javascript use parseFloat on the text value to get a number. Similarly in VBScript use CDbl on the text value. Both should conform to the current locale settings enforce for the user.

AnthonyWJones
Well, CDbl don't work fine. If, for example, you try to convert "3,5" using the wrong Locale, the result won't be 3, but 35 which makes comparison impossible.
gizmo
@gizmo: Are you doing this client-side or server-side? I can't see why the "wrong locale" would be used client-side.
AnthonyWJones
Because, for exemple, it's not because you're a french guy, writing your decimal with a comma, that the locale of your computer is well configured. We have to test if a decimal number make sens, whathever is the system locale. I know it won't work if there is 3 decimals, but that's another problem
gizmo
+1  A: 

[Edit after comment]

  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
KooiInc
Hum... And what with numbers like "3,123.56" or "3.123,56"
gizmo
See the edited answer
KooiInc
A: 

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

I wouldn't know how to do it in javascript or vbscript exactly though.

GoodEnough
A: 

I come form country where people write floats like this: 3.123,56. However it is not recomended (by linguists) to use . as a thousand separator. So we are fine without it.

What I would do is to forbid using any thousend separator. Give user an example: "Reals should look like this: 3123.56 od 3123,56". Then simply change , to . and parse it or use localized float parse.

You can always tell user that he did something wrong, maybe by giving information like this: "I'm sorry, but I'm only a stupid web aplication and I don't understand what you mean by **. Please help me and be more accurate" :).

klew