Solved

Javascript double datatype needed ?

Posted on 2009-04-14
1,514 Views
Hi ,
I have a value which converts into NaN while converting using parseFloat() function ,but as of the value range is more its not working.

So is there any other alternate like "parseDouble()" or sort of or any other suggestion or logic to handle this.

``````function add()

{

var ch1 = '1212154.45'; //Comes from textbox

var ch2 = '1212154454.45'; //Comes from textbox

var total = parseFloat(ch1) + parseFloat(ch2)

}
``````
0
Question by:vikasbapat
• 3

LVL 31

Accepted Solution

GwynforWeb earned 250 total points
Use the function isNaN() to detect if they are valid numbers and simple convert to rels by multiplying by 1

<script>
{
var ch1 = '1212154.45'; //Comes from textbox
var ch2 = '1212154454.45'; //Comes from textbox

if (isNaN(ch1) || isNaN(ch2) ) return 'not valid'
return 1*ch1 + 1*ch2

}
</script>
0

LVL 20

Expert Comment

Normally I have read somewhere, that they should convert automatically when used with a mathematical operation.
0

LVL 31

Expert Comment

typo, should read  'reals'  not  'rels'"
0

Author Closing Comment

ok thank you  i will try that example
0

LVL 31

Expert Comment

0

Featured Post

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…