Solved

convert decimal to string, internationally (points and commas)

Posted on 2004-10-21
1,647 Views
Last Modified: 2012-06-27
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
0
Question by:Ozzymandias
    12 Comments
     
    LVL 33

    Expert Comment

    by:knightEknight
    force them to enter only numeric characters and .

    <INPUT type="text" name="numeric" onkeyup='this.onchange();' onchange='this.value=this.value.replace(/[^\d\.]*/gi,"");' />
    0
     
    LVL 33

    Expert Comment

    by:knightEknight
    ... with a note about using . as decimal (instead of ,)
    0
     
    LVL 33

    Expert Comment

    by:knightEknight
    FYI also, your IF statement is malformed ...

    if (1*document.formname.fieldname.value>1000) {
    0
     
    LVL 14

    Accepted Solution

    by:
    What you're asing is impossible.  If the user enters "123,456" you don't know whether that comma is meant as a thousand separator or a decimal separator.

    You need to either do as knightEknight suggests and limit the kind of input, or add another control (say, a pair of radio buttons) where the user selects his preferred number format and use that to tell you how to interpret his input.
    0
     

    Author Comment

    by:Ozzymandias
    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
    0
     

    Author Comment

    by:Ozzymandias
    sorry, to clarify: Is it possible to get the regional settings from the user's pc USING JAVASCRIPT?

    Ozzy
    0
     
    LVL 33

    Assisted Solution

    by:knightEknight
    try this:

    if ( navigator.systemLanguage=="en-us" )
    {
       // use . as decimal
    }
    else
    {
       // use , as decimal
    }


    if systemLanguage doesn't work try navigator.userLanguage
    0
     
    LVL 33

    Expert Comment

    by:knightEknight
    ... assuming you only need this to work on IE, correct?
    0
     

    Author Comment

    by:Ozzymandias
    correct...

    in jscript a point is always used as DECIMAL separator, correct?
    0
     
    LVL 14

    Expert Comment

    by:RichieHindle
    [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.
    0
     
    LVL 33

    Expert Comment

    by:knightEknight
    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
    0
     
    LVL 33

    Expert Comment

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

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
    Article by: DanRollins
    This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    934 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now