Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript parseFloat and nulls

I am very new to javascript as I am currently making a cross platform web app in jQuery Mobile, I have used the example of XML Parsing to a HighCharts graph yet when I encounter a null in my series data it fails to draw any of the line and makes it into a scatter plot almost.

// push data points
$(series).find('data point').each(function(i, point) {
    seriesOptions.data.push( 
        parseFloat($(point).text())
    );
});

I have no idea how to write a if statement that checks to see if it found a null and if so how to tell it to use it... Can anyone please help or point me in the right direction as I would love my charts to be correct rather than placing a zero value where I have a null.

like image 532
Palendrone Avatar asked Mar 18 '13 13:03

Palendrone


People also ask

What does parseFloat mean in JavaScript?

The parseFloat() function is used to accept the string and convert it into a floating-point number. If the string does not contain a numeral value or If the first character of the string is not a Number then it returns NaN i.e, not a number.

What can I use instead of parseFloat?

If s does not begin with a number that parseFloat( ) can parse, the function returns the not-a-number value NaN . Test for this return value with the isNaN( ) function. If you want to parse only the integer portion of a number, use parseInt( ) instead of parseFloat( ) .

What is the difference between parseInt and parseFloat?

parseInt is for converting a non integer number to an int and parseFloat is for converting a non float (with out a decimal) to a float (with a decimal). If your were to get input from a user and it comes in as a string you can use the parse method to convert it to a number that you can perform calculations on.

Does parseFloat throw exception?

Yes. Apart from reference errors (because price was not declared) or parseFloat was overwritten with something that's not a function or the like, the builtin parseFloat can also throw exceptions. It does however never throw an error when you pass in a string.


2 Answers

Well, parseFloat will return 'NaN' if it's not a number (null and undefined are NaNs) so you could try doing like this:

// push data points
$(series).find('data point').each(function(i, point) {
    var floatVal = parseFloat($(point).text());
    if (!isNaN(floatVal)) {
        seriesOptions.data.push(floatVal);
    }
});
like image 175
Björn Roberg Avatar answered Jan 02 '23 06:01

Björn Roberg


A null check in JavaScript if just like any other C-style language:

 if (thing == null) 

Or

 if (thing != null)

I find this works well in most cases against my own programming where I'm writing as I would in, say, C#; however I find other peoples code relies on things never having been declared or set and such and so, and, all in all, it boils down to a spaghetti of checking for null and "undefined" - yes, the literal string, really - and whatever else.

like image 30
Grant Thomas Avatar answered Jan 02 '23 08:01

Grant Thomas