Skip to content Skip to sidebar Skip to footer

Division Produces Nan

I'm trying to divide the variable number by 100 and get NaN. I'm new to jQuery, can anyone help me to understand what I'm doing wrong? $.each(data.products, function (i, data) {

Solution 1:

First you're setting number to a string:

number = '<div class="number">'+ data.price + '</div>';

Then you are dividing that string by 100

In JavaScript that results in NaN which is short for Not a Number . This is specified in the spec.

A possible solution could be to divide the number itself by 100, and not the HTML string, for example:

varnumber = data.price/100;
$('#myElement').text(number);

This would not attach a nested div to your #myElement though.

Solution 2:

You can do it like this

number = '<div class="number">' + (data.price/100) + '</div>';
    $('#myElement').text((number ));

This is what you are doing in .text()

('<div class="number">' + (data.price/100) + '</div>')/100

Post a Comment for "Division Produces Nan"