Solved

Dutch language problem

Posted on 1997-05-23
2
417 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 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.
 
<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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…

706 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

18 Experts available now in Live!

Get 1:1 Help Now