Solved

# Dutch language problem

Posted on 1997-05-23
417 Views
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
Question by:Esther052397

LVL 3

Accepted Solution

RM032397 earned 100 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.

<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>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
//--end-->
</SCRIPT>

</BODY></HTML>
0

Author Comment

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be \$37.1B.
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…