Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Dutch language problem

Posted on 1997-05-23
2
Medium Priority
?
442 Views
Last Modified: 2012-06-22
I have discovered that the build-in function parseFloat cannot convert floating-point numbers <well strings containing them> when using the dutch preference in your windows setup.

This is because dutch preferences show floating point numbers not with a dot <.> but with a <,>.
Is there an answer to solve this problem or do I have to write a function "<,> to <.>" all by myself.
<I'm not an expert in JavaScript yet, so any help with the function or standard solution is appreciated>.

Thanks,
Esther Barthel <the netherlands>
0
Comment
Question by:Esther052397
2 Comments
 
LVL 3

Accepted Solution

by:
RM032397 earned 200 total points
ID: 1265763
Hello from England, Esther!
I find your question very interesting.
No you don't have to write the function yourself, because I have done it for you - well it's a start anyway.

I assume that numberstring is input into an INPUT type="text"
I have first assumed input of "1234,56"
The first function will convert that to a floating number by the following method:
Determine position of ","
Extract string before ","   A
Extract string aafter ","   B
return parseFloat of A + "." + B
But this ignores the possiblity of the input string having a "." and not a ",", or being a whole number (integer) with no decimal places. So the second function, slightly more complicated, deals with that.

Yes, you can write much more complex functions to check that input is numeric, but such functions are standard in most books.
 
<HTML><HEAD>
<TITLE>Dutch Floating Point</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
// "Dutch Floating Point" is comma "," and not point "."

function parseNfloat1(S) {
// returns floating point number from numberstring S written with "," for decimal.
// but will return NaN (Not A NUMBER) for any numberstring S written with "." for decimal!
// and also returns NaN for integer-strings like "1234"
var L    = S.length
var P    = S.indexOf(",")
return( parseFloat(S.substring(0,P)+"."+S.substring(P+1,L)) )
}

function parseNfloat(S) {
// returns floating point number from numberstring S written with "," for decimal.
// but will also return correct value for any numberstring S written with "." for decimal
// and will return correct value for integer strings too
var L    = S.length
var P    = S.indexOf(",")
 if  (P==-1) return  parseFloat(S);
 else return( parseFloat(S.substring(0,P)+"."+S.substring(P+1,L)) )
}

//--end-->
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
// alert(parseNfloat1("1234,56"))
// alert(parseNfloat1("1234.56"))
alert(parseNfloat("1234,56"))
alert(parseNfloat("1234.56"))
alert(parseNfloat("-1234,56"))
alert(parseNfloat("-1234.56"))
alert(parseNfloat("1234"))
//--end-->
</SCRIPT>

</BODY></HTML>
0
 

Author Comment

by:Esther052397
ID: 1265764
Wow, thanks a whole lot for your answer, really great to have you work out the function for me, very much apreciated as now I will strike with the credit for it at work......as I'm just getting back into the whole programming languages, this is helping me to get a whole lot further, so again many thanks for your help.
Esther Barthel
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…
Suggested Courses

916 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