Link to home
Start Free TrialLog in
Avatar of Ozzymandias
Ozzymandias

asked on

convert decimal to string, internationally (points and commas)

Hello all,

on a webpage I need to compare the value of an input field to a number value in script. I am sure that a number was entered (I wrote a long RegExp).

Two problems:

1) the site is international, so users in the USA may enter 123,456,789.123, while the same number is entered in Europe as 123.456.789,123. Furthermore, users are not forced to enter the digit grouping... They might as well just enter 123456789.123 or 123456789,123... How can I be sure that I get the correct value back?

2) I can't figure out how to do the conversion. If I use parseFloat, I always get back the first grouping of numbers, which is always smaller than 1000.

The code:

var strRE = /^\d{0,3},(\d{3},)*\d{3}(\.*\d)?$|^\d*(\.*\d)?$|^\d{0,3}\.(\d{3}\.)*\d{3}(,*\d)?$|^\d*(,*\d)?$/;
var re = new RegExp(strRE);

if (document.formname.fieldname.value.match(re)) {
    alert("Successful match");
   
    if (document.formname.fieldname.value.parseFloat>1000) {
        alert("bigger");
    } else {
        alert("smaller");
    }
}

Hope you can help me out !

SpSp
Avatar of knightEknight
knightEknight
Flag of United States of America image

force them to enter only numeric characters and .

<INPUT type="text" name="numeric" onkeyup='this.onchange();' onchange='this.value=this.value.replace(/[^\d\.]*/gi,"");' />
... with a note about using . as decimal (instead of ,)
FYI also, your IF statement is malformed ...

if (1*document.formname.fieldname.value>1000) {
ASKER CERTIFIED SOLUTION
Avatar of RichieHindle
RichieHindle

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ozzymandias
Ozzymandias

ASKER

thanx for your tips.
Is it possible to get the regional settings from the user's pc? That way, I could use RegExp to change e.g. points to #, and commas to @, save that value and display the value according to the user's settings...
All this is of course very labourous, but, as my customer says: "it is possible in Access, so why not in Internet Explorer, that's also a Microsoft program"... So even though using radio buttons or forcing to use eiter european or english notation are the more logic way of doing things, this would mean an extra action for the users, which has to be avoided...
Ozzy
sorry, to clarify: Is it possible to get the regional settings from the user's pc USING JAVASCRIPT?

Ozzy
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
... assuming you only need this to work on IE, correct?
correct...

in jscript a point is always used as DECIMAL separator, correct?
[knightEknight]
> if ( navigator.systemLanguage=="en-us" )

There are English-speaking countries other than the US that use 1,234.56 as their number format, you know.  8-)

Using the language is not going to work.  You could probably assume that any English-speaking country uses 1,234.56, but you can't assume the opposite.  Most African countries seem to use 1,234.56 - Afrikaans, Swahili and Zulu all do.  There are some languages that use spaces as thousands separators, eg. Latvian and Czech.

My preferred solution would be to ban thousand-separators and return an error if the input contains more than one non-digit character.
yes, I know ... I wasn't trying to slight anyone, I simply offered that as an example of what shows up in my browser.

>> in jscript a point is always used as DECIMAL separator, correct?

Yes, so if the language is "en-us" (or other . decimal languages, as Richie points out) then you can safely remove all commas from the string
Else, convert the comma to a dot
>> in jscript a point is always used as DECIMAL separator, correct?

actually, I don't know that for a fact.