[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

convert decimal to string, internationally (points and commas)

Posted on 2004-10-21
12
Medium Priority
?
1,656 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
Comment
Question by:Ozzymandias
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
12 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 12371243
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
ID: 12371261
... with a note about using . as decimal (instead of ,)
0
 
LVL 33

Expert Comment

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

if (1*document.formname.fieldname.value>1000) {
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Accepted Solution

by:
RichieHindle earned 189 total points
ID: 12371338
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
ID: 12399501
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
ID: 12399513
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
knightEknight earned 186 total points
ID: 12399646
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
ID: 12399668
... assuming you only need this to work on IE, correct?
0
 

Author Comment

by:Ozzymandias
ID: 12399889
correct...

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

Expert Comment

by:RichieHindle
ID: 12399893
[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
ID: 12399937
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
ID: 12399945
>> in jscript a point is always used as DECIMAL separator, correct?

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

650 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